I have a feeling this is going to be a stupid question, but I'm completely baffled.
I'm using the XRegExp library for Javascript to generate a regular expression that matches any word in any alphabet, and does not match words with numbers, punctuation or special characters. The library is found here: http://xregexp.com/
This is what I have so far:
var tests = [
"Hello",
"Hello789",
"Hello£*&£",
"你好世界",
"你好世界((£&"
];
var reg = XRegExp('^\\p{L}+$');
for (var i = 0; i < tests.length; i++) {
console.log (tests[i] + " : " + (reg.test(tests[i]) ? "true" : "false"));
}
This produces the following output:
Hello : true
Hello789 : false
Hello£*&£ : false
你好世界 : true
你好世界((£& : false
I know that reg
is matching the right thing, but how do I use it in a replace so that the numbers, punctuation and special characters are stripped out? The output I want is :
Hello : Hello
Hello789 : Hello
Hello£*&£ : Hello
你好世界 : 你好世界
你好世界((£& : 你好世界
These are the things I've tried so far, with no success:
XRegExp.replace(tests[i], '^\\p{L}+$', ''));
XRegExp.replace(tests[i], '\\p{L}+$', ''));
XRegExp.replace(tests[i], '^\\p{L}', ''));
XRegExp.replace(tests[i], '\\p{L}', ''));
All of these return the same string I gave them, with no changes at all. Any one have an idea what I'm doing wrong?
Your regular expression is matching only letters, but it sounds like you want to match everything except letters:
Then you can replace everything that matches with the empty string.