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 Overflow
Discussions

javascript: how to remove comma(,) between two special character using regex - Stack Overflow
i know this is very simple question but i start learning reg-ex, so please provide solution.. i want to remove comma if exist between @ and .(dot) from the email id provided by user. input abc@... More on stackoverflow.com
🌐 stackoverflow.com
javascript - RegEx- allow alpha,numeric with only special char Comma - but no precceeding or succeeding commas - Stack Overflow
i need help in forming an regular expression that allow Alpha,Numeric and only special character comma - but comma should not be present at starting or ending or no middle blank commas.. Right now ... More on stackoverflow.com
🌐 stackoverflow.com
October 19, 2016
node.js - Replace comma with special characters in Javascript - Stack Overflow
I have a requirement where I want to replace all occurrences of ',' with '\,'. It works if I try to replace with other characters/strings, but seems to have some issue with '\,'. 'http://example.co... More on stackoverflow.com
🌐 stackoverflow.com
regex - match string with comma javascript - Stack Overflow
I want to match string with or without comma and space: "hello world" "hello,world" "hello world," ",hello world" ",hello,world," ... More on stackoverflow.com
🌐 stackoverflow.com
November 30, 2016
🌐
freeCodeCamp
forum.freecodecamp.org › programming › javascript
Replacing space and comma from a string using regex - JavaScript - The freeCodeCamp Forum
October 26, 2020 - I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: “23,45,567”.replace(‘/[1]$/g’, ‘’) “23,45,567”.replace(‘/[2]+$/g’, ‘’) “23,45,567”.replace(‘/^\s,$/g’, ‘’) And many ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions › Cheatsheet
Regular expression syntax cheat sheet - JavaScript | MDN
This page provides an overall cheat sheet of all the capabilities of RegExp syntax by aggregating the content of the articles in the RegExp guide. If you need more information on a specific topic, please follow the link on the corresponding heading to access the full article or head to the guide. Character classes distinguish kinds of characters such as, for example, distinguishing between letters and digits.
🌐
Stack Overflow
stackoverflow.com › questions › 67499160 › replace-comma-with-special-characters-in-javascript
node.js - Replace comma with special characters in Javascript - Stack Overflow
In regex, the '\' char is used to escape other chars to make them literal. to make this char literal you need to escape it as well. \\,. ... I don't know your use case, but "\" isn't a valid URL character unless it's properly encoded.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › replace-commas-with-javascript-regex
Replace commas with JavaScript Regex?
We want to replace the last comma in each string with " JavaScript". The regular expression /,(?=[^,]*$)/ matches a comma that is followed by any characters except commas until the end of the string, effectively targeting the last comma.
Top answer
1 of 3
4
/([\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>...*/
2 of 3
1

Try this for your regex:

var re = /([,|\s]+)?world([,|\s]+)?/g; 
🌐
Regex Tester
regextester.com › 95468
Comma and Slash - Regex Tester/Debugger
Url checker with or without http:// or https:// Match string not containing string Check if a string only contains numbers Only letters and numbers Match elements of a url Url Validation Regex | Regular Expression - Taha Match an email address Validate an ip address nginx test match whole word Match or Validate phone number Match html tag Find Substring within a string that begins and ends with paranthesis Match dates (M/D/YY, M/D/YYY, MM/DD/YY, MM/DD/YYYY) Blocking site with unblocked games Empty String Match if doesn't start with string Checks the length of number and not starts with 0 Match a valid hostname Not Allowing Special Characters · Regex Tester isn't optimized for mobile devices yet.
🌐
ServiceNow Community
servicenow.com › community › itsm-forum › regex-expression-to-check-comma-separated-string › m-p › 471373
Solved: Re: Regex expression to check comma separated stri... - ServiceNow Community
February 3, 2022 - Hi, My code should work, I have tested in my PDI. Anyway, you can add space in between '9' and ']' that will make it validate space as well. As below, I have made that part bold there is space. var regex = (/^(([a-zA-Z0-9 ](,)?)*)+$/) Let me know if you have any further queries.
🌐
ServiceNow Community
servicenow.com › community › it-service-management-forum › regex-expression-to-check-comma-separated-string › m-p › 471369
Solved: Regex expression to check comma separated string - ServiceNow Community
February 3, 2022 - You can try other way around, below script will written false for your first two scenario and it will return true whenever there will be any special character in string. ... You can use not (!) with result to get your required output. ... Let me know if you have any further queries. Please mark this as Correct or Helpful if it helps. ... By marking my response as correct or helpful, you contribute to helping future readers with similar issues. Regards, Abhijit ServiceNow MVP ... Thank you for the response. The script which you gave didnt worked. I have used this regExp and it is working fine.
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript replace all word | space, comma | special characters
How to JavaScript replace all Word | Space, Comma | Special Character
May 15, 2021 - <!DOCTYPE html> <html> <script> var string = 'EyeHunts Tutorial JS'; var newstring = string.replace(/JS/, 'Javascript'); document.write(newstring); </script> <body> </body> </html> ... The best way is to use regular expression with g (global) flag. ...
🌐
Reddit
reddit.com › r/learnprogramming › [regex] help with a regex for single letters separated by comma/whitespace?
r/learnprogramming on Reddit: [Regex] Help with a regex for single letters separated by comma/whitespace?
April 9, 2021 -

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] *)

🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions
Regular expressions - JavaScript - MDN Web Docs
March 17, 2026 - Use the constructor function when ... of simple characters, such as /abc/, or a combination of simple and special characters, such as /ab*c/ or /Chapter (\d+)\.\d*/. The last example includes parentheses, which are used as a memory device....
🌐
Mp3tag Community
community.mp3tag.de › support
Escaping commas in regular expressions - Support - Mp3tag Community
February 9, 2011 - I want to use a comma in my regular expression, so I tried this: $regexp($meta(artist),^(.), (.)$,$1, and $2) It supposed to take multiple artist fields and produce something like ‘Artist 1, Artist 2, and Artist 3’ Bu…