js replace replaceAll

Recently, when I was using js replace, I thought that I wanted to replace all unconsciously, so I used replaceAll. When I ran the code, I found out that replaceAll is a Java code.

 

var str = '1,999.00';
document.write(str.replace(',',''));

 Let's talk about how to replace all the several methods in js:

 

   give the syntax definition of replace

  stringObj.replace(rgExp, replaceText)

  Where stringObj is a string (string), reExp can be a regular expression object (RegExp) or a string (string), and replaceText is the string that replaces the found string.

1. Regular

var str = '1,000,999.00';
document.write(str.replace(new RegExp(/,/g),''));//结果1000999.00

 If you want to achieve dynamic replacement, you can replace the string to be replaced with a variable and it's ok

var str = '1,000,999.00';
var temp = ',';
document.write(str.replace(new RegExp(temp,'gm'),''));

 2. The method of parsing strings with js

var str = '1,000,999.00';
document.write(str.split(',').join(''));//result 1000999.00

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326374552&siteId=291194637
Recommended