How to replace all decimal points in a string with other symbols in js

You can use the replace method of js to replace the dot with other characters:

It should be noted that the replace method accepts two parameters: the string or regular expression to find, and the string to replace with. The second parameter can also be a function, in which case the function will accept two parameters: the string to match and the number of the matching position.

For example: replace the decimal point in the following string with a "-" character

let str = '测试1.6.4';
str = str.replace(/\d+\./g, function(match) {
  return match.replace(/\./g, '-');
});
console.log(str);  // 输出: 测试1-6-4

Basically everything is universal!

Guess you like

Origin blog.csdn.net/qq_42174597/article/details/128614248