I need to replace all digits.
我需要替换所有的数字。
My function only replaces the first digit.
我的函数只替换第一个数字。
var s = "04.07.2012";
alert(s.replace(new RegExp("[0-9]"), "X")); // returns "X4.07.2012"
// should be XX.XX.XXXX"
4 个解决方案
#1
52
You need to add the "global" flag to your regex:
您需要在您的regex中添加“global”标志:
s.replace(new RegExp("[0-9]", "g"), "X")
or, perhaps prettier, using the built-in literal regexp syntax:
或者,使用内置的文字regexp语法更漂亮:
.replace(/[0-9]/g, "X")
#2
7
Use
使用
s.replace(/\d/g, "X")
which will replace all occurrences. The g
means global match and thus will not stop matching after the first occurrence.
它将取代所有发生的事情。g表示全局匹配,因此在第一次匹配之后不会停止匹配。
Or to stay with your RegExp
constructor:
或者继续使用RegExp构造函数:
s.replace(new RegExp("\\d", "g"), "X")
#3
2
The /g
modifier is used to perform a global match (find all matches rather than stopping after the first)
/g修饰符用于执行全局匹配(查找所有匹配,而不是在第一个匹配之后停止)
You can use \d
for digit, as it is shorter than [0-9]
.
您可以使用\d为数字,因为它比[0-9]更短。
JavaScript:
JavaScript:
var s = "04.07.2012";
echo(s.replace(/\d/g, "X"));
Output:
输出:
XX.XX.XXXX
#4
2
find the numbers and then replaced with strings which specified. It is achieved by two methods
找到数字,然后用指定的字符串替换。它是通过两种方法实现的
-
Using a regular expression literal
使用正则表达式文字
-
Using keyword RegExp object
使用关键词正则表达式对象
Using a regular expression literal:
使用正则表达式文字:
<script type="text/javascript">
var string = "my contact number is 9545554545. my age is 27.";
alert(string.replace(/\d+/g, "XXX"));
</script>
**Output:**my contact number is XXX. my age is XXX.
**输出:**我的联系电话是XXX。我的年龄是XXX。
for more details:
更多细节:
http://www.infinetsoft.com/Post/How-to-replace-number-with-string-in-JavaScript/1156
http://www.infinetsoft.com/Post/How-to-replace-number-with-string-in-JavaScript/1156
#1
52
You need to add the "global" flag to your regex:
您需要在您的regex中添加“global”标志:
s.replace(new RegExp("[0-9]", "g"), "X")
or, perhaps prettier, using the built-in literal regexp syntax:
或者,使用内置的文字regexp语法更漂亮:
.replace(/[0-9]/g, "X")
#2
7
Use
使用
s.replace(/\d/g, "X")
which will replace all occurrences. The g
means global match and thus will not stop matching after the first occurrence.
它将取代所有发生的事情。g表示全局匹配,因此在第一次匹配之后不会停止匹配。
Or to stay with your RegExp
constructor:
或者继续使用RegExp构造函数:
s.replace(new RegExp("\\d", "g"), "X")
#3
2
The /g
modifier is used to perform a global match (find all matches rather than stopping after the first)
/g修饰符用于执行全局匹配(查找所有匹配,而不是在第一个匹配之后停止)
You can use \d
for digit, as it is shorter than [0-9]
.
您可以使用\d为数字,因为它比[0-9]更短。
JavaScript:
JavaScript:
var s = "04.07.2012";
echo(s.replace(/\d/g, "X"));
Output:
输出:
XX.XX.XXXX
#4
2
find the numbers and then replaced with strings which specified. It is achieved by two methods
找到数字,然后用指定的字符串替换。它是通过两种方法实现的
-
Using a regular expression literal
使用正则表达式文字
-
Using keyword RegExp object
使用关键词正则表达式对象
Using a regular expression literal:
使用正则表达式文字:
<script type="text/javascript">
var string = "my contact number is 9545554545. my age is 27.";
alert(string.replace(/\d+/g, "XXX"));
</script>
**Output:**my contact number is XXX. my age is XXX.
**输出:**我的联系电话是XXX。我的年龄是XXX。
for more details:
更多细节:
http://www.infinetsoft.com/Post/How-to-replace-number-with-string-in-JavaScript/1156
http://www.infinetsoft.com/Post/How-to-replace-number-with-string-in-JavaScript/1156