This will match everything that is not numeric or not a comma or period (decimal point)
var result = str.replace(/[^0-9\.,]/g, "");
Answer from Ding on Stack OverflowThis will match everything that is not numeric or not a comma or period (decimal point)
var result = str.replace(/[^0-9\.,]/g, "");
var check = yourString.match(/[^0-9,\.]/);
Here check will be 'null' if the string does not contain a character different to a number, a comma or a point. If the string has any of these characters, check will be an Array. You could test this in this way
if (check === null ) { console.log('No special characters present') };
if (typeof check === 'Array' ) { console.log('Special characters present') };
var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g
Should work
Also may want to have a minimum length i.e. 6 characters
var regex = /^[a-zA-Z0-9!@#\
/g
a sleaker way to match special chars:
/\W|_/g
\W Matches any character that is not a word character (alphanumeric & underscore).
Underscore is considered a special character so add boolean to either match a special character or _
javascript: how to remove comma(,) between two special character using regex - Stack Overflow
javascript - RegEx- allow alpha,numeric with only special char Comma - but no precceeding or succeeding commas - Stack Overflow
node.js - Replace comma with special characters in Javascript - Stack Overflow
regex - match string with comma javascript - Stack Overflow
You can use:
email = email.replace(/@([^.]+)\./g, function(text, p1) {
return text.replace(/,+/g, '');
});
One of the examples:
'abc@gm,ail.com, xyz@nauk,ri.com, srs@y,ahoo.com, efgh@hot,mail.com'.replace(/(@[^\s,]+),([^\s\.].)/g, '
2')
Two:
'abc@gm,ail.com, xyz@nauk,ri.com, srs@y,ahoo.com, efgh@hot,mail.com'.replace(/,([^\s])/g, '$1')
/([\s,]\s*world\s*,?)/i
[\s,] let's us either have a space or a comma to ensure there is a separation between the words. Then following it with \s* allows for zero to many white spaces between the comma/space and the word world. We then follow that up with some more \s* before the ,? which means one or none commas. Then we capture () it with a case insensitive, i, regular expression.
This should handle the replacing.
<style>
.highlighted {
background-color: rgb(100,100,255);
color: white;
}
</style>
<div id = "0">
"hello world"
<br>
"hello,world"
<br>
"hello world,"
<br>
",hello world"
<br>
",hello,world,"
<br>
", hello , world ,"
</div>
<div id="1"></div>
<script>
var content = document.getElementById("0").innerHTML.split("<br>"),
result = [];
for(var i in content) {
// First extract what we want.
var world = /([\s,]\s*world\s*,?)/.exec(content[i])[1],
// Split everything up to make the insertion easier.
hello = content[i].split(world);
// Place the result back.
result.push(hello.join('<span class="highlighted">'+world+'</span>'));
}
document.getElementById("1").innerHTML = result.join("<br>");
</script>
UPDATE To fit what @jsem asked down in the comments, I created a function that will determine the correct phrase, such as "hello world" or the many others based off of the search and punctuation.
function separate(search, punc) {
if(typeof punc !== "string") punc = ",";
var phrase = new RegExp(".*?(\\w+)\\s*(?:"+punc+"|\\s)\\s*(\\w+)\\s*(?:"+punc+")?","i").exec(search);
if(!phrase) throw new Error("Search passed could not be parsed.");
return {word1:phrase[1], word2:phrase[2]};
};
Then used the information gathered from that to create a unique regular expression that will grab the information to be highlighted.
function build_regex(phrase, punc) {
if(typeof punc !== "string") punc = ",";
return new RegExp("^.*?"+phrase.word1+"\\s*((?:"+punc+"|\\s)\\s*"+phrase.word2+"\\s*(?:"+punc+")?).*$", "i");
};
With these two functions, I created a highlighting function using the same splitting and joining algorithm as before.
function highlight(sentence, search, punc) {
if(typeof punc !== "string") punc = ",";
var highlighted = build_regex(separate(search, punc), punc).exec(sentence)[1],
remains = sentence.split(highlighted);
return remains.join('<span class="highlighted">'+highlighted+'</span>');
};
Ex:
highlight("This is my sentence hello, world!", "hello world");
/* Output: This is my sentence hello<span class="highlighted">, world</span>!*/
highlight("Change things up... sir.", "up sir", "\\.\\.\\.");
/* Output: Change things up<span class="highlighted">... sir</span>.*/
highlight("Give me some options? ummm...", "options! ummm", "\\?|\\!");
/* Output: Give me some options<span class="highlighted">? ummm</span>...*/
Try this for your regex:
var re = /([,|\s]+)?world([,|\s]+)?/g;
You almost have it, you just left out 0 and forgot the quantifier.
word.matches("^[0-9,;]+$")
You are 90% of the way there.
^[0-9,;]+$
Starting with the carat ^ indicates a beginning of line.
The [ indicates a character set
The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ;.
The closing ] indicates the end of the character set.
The plus + indicates that one or more of the "previous item" must be present. In this case it means that you must have one or more of the characters in the previously declared character set.
The dollar $ indicates the end of the line.
You can use the \s whitespace character class repeated multiple times:
name = name.replace(/\s*,\s*/g, '*');
Try this following?
name = name.replace(/\s*,\s*/, '*');
@Bergi's answer also adds the g flag to catch all commas (with whitespace), so depending on your input, that may work as well.
My regex: ([a-zA-Z],? *)+
The desired input is something like "A, B, C, D". But of course I want to allow input where the letters aren't capitalized, or there's more than one space between a letter+comma, or no space, etc.
Here's the regex I have, but it's not quite right because input such as "A B C D" is still considered valid.
([a-zA-Z],? *)+ which as I understand it (I'm new to regex) means: contains any letter from a to z, case insensitive, followed by zero or one comma, followed by 0 or more space(s), and then 1 or more of this entire pattern.
Now the issue I think has to do with the "zero or one comma" because I want to allow the input to have multiple letters eg. "A, B, C" but the last character shouldn't be followed by a comma, because that's weird. It also needs to match with single letter input which doesn't have a comma either. So what I want is it to be like a letter followed by a comma only if this isn't the last letter or the only letter... but I don't know how to do that with regex.
EDIT: Here is a new regex I made. I think this works: ([a-zA-Z] *, *)*([a-zA-Z] *)