最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex pattern if string contains characters - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 2

as @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("ú"));

发布评论

评论列表(0)

  1. 暂无评论