admin管理员组文章数量:1430570
I have input box on my page which contains strings separated by + such as:
this+is+my+string
On page load i want to replace all + with a blank. I have tried:
var str = $('#MyText1');
str = str.replace(/+/gi, ' ');
However i am getting the following error:
SyntaxError: invalid quantifier
Please note my application is using Angular v.1.2.8
I have input box on my page which contains strings separated by + such as:
this+is+my+string
On page load i want to replace all + with a blank. I have tried:
var str = $('#MyText1');
str = str.replace(/+/gi, ' ');
However i am getting the following error:
SyntaxError: invalid quantifier
Please note my application is using Angular v.1.2.8
Share Improve this question asked Jul 17, 2014 at 11:44 Oam PsyOam Psy 8,66335 gold badges97 silver badges167 bronze badges 3-
1
"escape" the plus sign ->
str = str.replace(/\+/gi, ' ');
then it works. – davidkonrad Commented Jul 17, 2014 at 11:46 - @davidkonrad - With the above, i am seeing error: Error: str.replace is not a function Is this because i am using Angular? – Oam Psy Commented Jul 17, 2014 at 11:52
-
have posted an answer, you also forget to extract the value from the input, you just reference the input box itself, and that has no
.replace()
method. – davidkonrad Commented Jul 17, 2014 at 12:00
3 Answers
Reset to default 5+
is indeed a quantifier in regular expressions (meaning "1 or more").
You're close with your syntax -- try
str = str.replace(/\+/g, ' ');
You forget to extract the content of the input box :
var str = $('#MyText1').val();
^^^^^
now the escape will work :
str = str.replace(/\+/gi, ' ');
see demo -> http://jsfiddle/8TKWZ/
You are replacing using a regular expression. In a regular expression, '+' has special meaning. However, you don't want that special meaning, you want the literal character. As such, you must escape the '+'
str = str.replace(/\+/gi, ' ');
本文标签: jqueryJavascripthow to replace allin a string with a blank spaceStack Overflow
版权声明:本文标题:jquery - Javascript - how to replace all + in a string with a blank space - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745455229a2659071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论