PDA

View Full Version : replace()


jasonrg
12-06-2002, 06:31 PM
I am trying to replace the '+' character in a string with a space using the following code:

variable.replace(/+/g," ");

When this executes, I receive an 'Unexpected quantifier' error. Any suggestions would be greatly appreciated. Thanks!!

JasonRG

beetle
12-06-2002, 06:40 PM
The + is a special character used in regular expressions. These special characters are called metacharacters. The + metacharacter is a quantifier meaning "1 or more". So, to mach a plus sign, you need to escape it (as you need to with all metacharacters) to match it.

variable.replace(/\+/g," ");

Visit the links below for more on regular expressions
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/regexp.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsjsgrpRegExpSyntax.asp
http://www.webreference.com/js/column5/rules.html

jasonrg
12-06-2002, 06:43 PM
That's exactly what I was looking for! Thanks for the quick help!!

JasonRG