javascript replace matches % in str.replace(/%|\$\s?|(,*)/g, '') but not in str.replace(/\$\s?|(,*)|%/g, '')

I am trying to match \$\s*, , and % in a string, say $ 3,222 and 100%.

In chrome dev tool,

If I do +str.replace(/%|\$\s?|(,*)/g, ''). I get the correct result number.

But if I do a +str.replace(/\$\s?|(,*)|%/g, ''), I get NaN.

Any reason for this peculiar behaviour?

const str = "$ 32,333%"

console.log(+str.replace(/\$\s?|(,*)|%/g, ''))

console.log(+str.replace(/%|\$\s?|(,*)/g, ''))

How does "/\s/g" replace spaces with other characters?, It's a regular expression where the \s means "match whitespace" and the g is a string globally, so that str.replace will replace all occurrences of the pattern. Javascript has a few methods that use regular expressions, like� The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. Note: If you are replacing a value (and not a regular expression ), only the first instance of the value will be replaced.

This doesn't directly answer your question (@Wiktor's answer does a good job with that), but it seems to me your goal is to get all the digits out of the string and toss the rest. An easier alternative is to use a negative character class to match for anything that is not a digit:

var str = "$ 32,333%";
var num = +str.replace(/[^0-9]/g,'');
console.log(num); // 32333

String.prototype.replace(), The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. Syntax. const newStr = str .replace( regexp | substr , newSubstr | function ) F\b/g; return s.replace(test, convert); }� Syntax. The syntax to use the replace() method is as follows − string.replace(regexp/substr, newSubStr/function[, flags]); Argument Details. regexp − A RegExp object. The match is replaced by the return value of parameter #2.

const str = "$ 32,333%"
console.log(+str.replace(/\D/g, ''))

JavaScript String replace() Method, var res = str.replace("Microsoft", "W3Schools"); To replace all occurrences of a specified value, use the global (g) modifier (see "More Examples" below). The JavaScript replace() function takes two arguments: The string or regular expression to search for. The string to replace the matches found with. Replacing First Match Only: If we specify the first argument as string, the replace function only replaces the first occurrence of the string. Consider the example below: str.replace('hello', 'hi

3 Ways To Replace All String Occurrences in JavaScript, You can replace all occurrences of a string using split and join The regular expression literal /\s/g (note the g global flag) matches the space� I have this string: "Test abc test test abc test test test abc test test abc" Doing: str = str.replace('abc', ''); seems to only remove the first occurrence of abc in the string above.

Regular Expressions Cookbook, string resultString = outerRegex.Replace(subjectString, new MatchEvaluator( ComputeReplacement)); public String ComputeReplacement(Match matchResult )� In JavaScript, replace() is a string method that is used to replace occurrences of a specified string or regular expression with a replacement string. Because the replace() method is a method of the String object, it must be invoked through a particular instance of the String class.

JavaScript: Novice to Ninja, pdf $/; This looks for zero or more occurrences of any character, followed by split (/\s--/) // << ['Hello', 'World ! can use the g flag to return all the matches: ' JavaScript". match (/ [aeiou ) /g); is no match: "I'm learning JavaScript". search (/ruby/i); << –1 1:ks The replace () method replaces any matches with another string. The "g" that you are talking about at the end of your regular expression is called a "modifier". The "g" represents the "global modifier". This means that your replace will replace all copies of the matched string with the replacement string you provide. A list of useful modifiers: g - Global replace. Replace all instances of the matched string

  • Yes, there is a reason: ,* matches an empty string. Use ,+.
  • @WiktorStribiżew, How does ,* affect a match on %?