I'm not sure how to escape '+' in regex. Plus can come multiple times in i
so we need to replace all +
in the string. Here's what I have:
i.replace(new RegExp("+","g"),' ').replace(new RegExp("selectbasic=","g"),'').split('&');
But this gives me this error:
Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat
replace() method to replace all spaces in a string, e.g. str. replace(/ /g, '+'); . The replace() method will return a new string with all spaces replaced by the provided replacement.
To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag.
replaceAll() The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.
The +
character has special significance in regular expressions. It's a quantifier meaning one or more of the previous character, character class, or group.
You need to escape the +
, like this:
i.replace(new RegExp("\\+","g"),' ')...
Or more simply, by using a precompiled expression:
i.replace(/\+/g,' ')...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With