Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all non-word characters like ?*+#

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?

like image 842
BeMoreDifferent.com Avatar asked Nov 03 '12 13:11

BeMoreDifferent.com


People also ask

What are non-word characters?

Non-word characters include characters other than alphanumeric characters ( - , - and - ) and underscore (_).

Which method is used to match any non-word character?

The \W metacharacter matches non-word characters: A word character is a character a-z, A-Z, 0-9, including _ (underscore).

How do you replace a word in RegEx?

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.

What is RegEx in python?

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.


1 Answers

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, "")
like image 76
Tim Pietzcker Avatar answered Oct 05 '22 01:10

Tim Pietzcker