正则表达式不能用 .replace() 工作
Regex not working way it should with .replace()
嗨,我是jQuery的新手,但我得到了一个简单的脚本,它应该从字符串中删除整数,但它似乎只剥离了前2位数字?
想法?
var strValue = "5321 Test";
var regex = /([0-9])/;
strValue = strValue.replace(regex,'');
alert(strValue);
您可以使用全局正则表达式修饰符:
/[0-9]/g
g
: 全球。所有匹配项(在第一场比赛时不返回)。
或者,您可以将+
添加到正则表达式中,以定位所有整数,而不仅仅是第一个:
/([0-9]+)/;
0-9
:介于 0 和 9 之间的单个字符。
+
:在一次和无限次之间,尽可能多地。
或者也使用'd+
.
d
:匹配数字 [0-9]。
希望这有帮助。
var strValue = "5321 Test";
var regex = /([0-9])/g;
strValue = strValue.replace(regex,'');
alert(strValue);
你应该在正则表达式后面添加一个g,这意味着它必须替换所有出现的,而不仅仅是第一个。所以你的代码将是:
var strValue = "5321 Test";
var regex = /([0-9])/g;
strValue = strValue.replace(regex,'');
alert(strValue);
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- 正则表达式不能用 .replace() 工作
- .replace() 不再为我工作
- JS .replace(str, str):如何获取参数 1 中的变量并全局工作
- 如何使 window.location.replace 在 IE 9 中工作
- innerHTML.replace不是在firefox中工作,而是在IE中工作
- jQuery.attr('src')replace在FF中不工作
- javascript方法replace未按预期工作
- Window.location.replace不能在IE 7上工作
- 由于转义序列,Javascript Replace不能工作
- jquery .replace(/.)不是为我工作,而是为别人工作
- . replace(/““ n / g,““”;“+“ n")将这项工作
- .replace()在Firefox中工作
- Str.replace不能在html中工作
- .replace()不能在jQuery函数中工作
- Javascript替换/,str.replace不工作