Sorry for being an absolute beginner, when it es to Javascript and Regex,
I have a Codepen:
What I want to acplish is to validate, if a String contains some special UTF-8 characters. That's why I work with RegExp. The pattern I have here will return false only if the string to test equals one of the characters. But I want to return false if it contains one of these characters.
How can I acplish this, I know it should be quite easy, but I wasn't able to get it working.
var regEx = new RegExp('[\u0001-\u00FF]');
console.log("This should be true: " + regEx.test("Tes"));
console.log("This should be false: " + regEx.test("Tes�"));
console.log("This returns false, because the string equals a special character: " + regEx.test("�"));
Sorry for being an absolute beginner, when it es to Javascript and Regex,
I have a Codepen: http://codepen.io/anon/pen/dNJNvK
What I want to acplish is to validate, if a String contains some special UTF-8 characters. That's why I work with RegExp. The pattern I have here will return false only if the string to test equals one of the characters. But I want to return false if it contains one of these characters.
How can I acplish this, I know it should be quite easy, but I wasn't able to get it working.
var regEx = new RegExp('[\u0001-\u00FF]');
console.log("This should be true: " + regEx.test("Tes"));
console.log("This should be false: " + regEx.test("Tes�"));
console.log("This returns false, because the string equals a special character: " + regEx.test("�"));
Share
Improve this question
edited Jan 30, 2017 at 9:48
Thomas Ayoub
29.5k16 gold badges98 silver badges147 bronze badges
asked Jan 30, 2017 at 9:15
user5417542user5417542
3,4168 gold badges31 silver badges55 bronze badges
4
-
1
Just create a
<>
snippet instead of codepen as I just did for you – mplungjan Commented Jan 30, 2017 at 9:16 - Thanks :) Would have been the easiest, yes – user5417542 Commented Jan 30, 2017 at 9:17
- Your RegEx always returns true because 'T', 'e' and 's' are in the range you specified. Use this tool for regxs, it works great :) regex101./r/apZoYk/1 – Gabriel Commented Jan 30, 2017 at 9:22
- I suggest remove special character after check – Divyesh Kanzariya Commented Jan 30, 2017 at 9:53
2 Answers
Reset to default 2as @Gabriel mented, it's returning true because there's at least one character in the string that matches your range
what you want to do is check that every character is within the range
/^[\u0001-\u00FF]+$/
or that any character is not within the range
[^\u0001-\u00FF]
in the second case you'd have true when a special character is used and false when all characters are safe, so you probably have to flip the checks you do afterward
Why not the other way around?
See Regular expression to match non-English characters?
Also your test could be match
instead or a test
of the WHOLE string
var regEx = /[\x00-\x7F]/g; // can be added to
function okChar(str) {
var res = str.match(regEx);
if (res===null) return false;
return res.length===str.length;
}
console.log("This should be true: " + okChar("Tes"))
console.log("This should be false: " + okChar("Tesú"));
console.log("This returns false, because the string equals a special character: " + okChar("ú"));