I need a some help to replace all non-word characters in a string.
As an example (stadtbezirkspräsident'
should become stadtbezirkspräsident
.
This Regex should work for all languages so it's kind of tricky because I have no idea how to match characters like ñ
or œ
. I tried solving this with
string.replace(/[&\/\\#,+()$~%.'":*?<>-_{}]/g,' ');
but ther are still to many special characters like Ø
left.
Perhaps there is a general Selector for this, or anybody has solved this problem before?
Non-word characters include characters other than alphanumeric characters ( - , - and - ) and underscore (_).
The \W metacharacter matches non-word characters: A word character is a character a-z, A-Z, 0-9, including _ (underscore).
To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d. *\d/ , so it is replaced.
A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern.
If you have define all the Unicode ranges yourself, it's going to be a lot of work.
It might make more sense to use Steven Levithan's XRexExp
package with Unicode add-ons and utilize its Unicode property shortcuts:
var regex = new XRegExp("\\P{L}+", "g")
string = XRegExp.replace(string, regex, "")
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