5

I'm trying to strip a string to leave only word characters remaining. For anything using the Latin alphabet, I can manage it quite easily with

str = str.replace(/\W/g, '').replace(/[0-9]/g, '');

(I think I probably don't need both replaces, but I'm very new to regular expressions and not sure what I'm doing)

However, this also strips out foreign characters such as chinese or arabic.

How would I write a function to do this?

strOne = "test!(£)98* string";
strTwo = "你好,325!# 世界";

cleanUp (strOne); // Output: "test string"
cleanUp (strTwo); // Output: "您好 世界"

(In case anyone is wondering, the chinese is me running "hello world" through an online translator)

On a library note, I don't know if it's relevant but I'm using dojo and would like to avoid jquery if possible.

2
  • I suppose you can look into unicodes like in this SO post, another link or you can convert the characters to english using a plugin then cleanup, the only plugin I can think for now is a jquery one though :) google's translate plugin
    – user2587132
    Sep 6, 2013 at 10:31
  • 1
    To merge both replaces you can use |. The pipe character means this or that (this|that), so in your case the regex would be /\W|[0-9]/g.
    – Ron
    Sep 6, 2013 at 10:43

2 Answers 2

4

you need a regex pattern using unicode character properties, namely \P{Letter}.

unfortunately the native js regex engine does not support these constructs (cf. mdn docs). however there is (at least) this third-party library which includes a js plugin adding the support.

code sample:

var regex, str;

str = "whatever";

regex = XRegExp('\\P{Letter}'); 
str   = XRegExp.replace(str, regex, '');
3
  • I'm afraid third-party libraries are rather slow, but this is not a problem for e.g. amount of text entered by user in html form.
    – kirilloid
    Sep 6, 2013 at 10:43
  • Thank you, that looks like what I want! Sorry for being a pain, but how would I use it? I've not come across using plugins before.
    – Emma
    Sep 6, 2013 at 10:44
  • have a look at their website. you only have to include the js files they mention with script tags as you'd do with any other external js. the plugin mechanism is implemented using native js language features (objects, prototypes, closures). for details inspect their (un-minified) source code
    – collapsar
    Sep 6, 2013 at 10:47
1

\W is equivalent to [^a-zA-Z_0-9]

instead you need to list all the characters that you want to strip out.

str = str.replace(/[put the characters you want to get rid of here]*/g, '');

2
  • 1
    All of them? Including every kind of special character in every alphabet? Surely there'd be a better way than that. Apart from anything, I have no idea what the special characters are in other alphabets!
    – Emma
    Sep 6, 2013 at 10:28
  • @Emma: you could resort to unicode code point ranges. the task could still be ambitious, however, it might be viable if you do not need the most general solution of accepting string from every alphabet represented in unicode.
    – collapsar
    Sep 6, 2013 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.