简体   繁体   English

JavaScript正则表达式查找第一个不等于

[英]JavaScript Regex find first not equal to

I have, what I thought was, a simple regex to get anything not (letter), (number) or (%) 我本来以为是一个简单的正则表达式,以获取不包含(字母),(数字)或(%)的任何内容

str.indexOf(/[^a-zA-Z0-9%]/);

However, I'm ALWAYS getting -1 . 但是,我总是得到-1

Here's an example str : 这是str的示例:

Check out this awesome video!

What am I doing wrong? 我究竟做错了什么?

You cannot put regex into indexOf. 您不能将正则表达式放入indexOf中。 Use search instead. 请改用搜索。

See this for more info: Is there a version of JavaScript's String.indexOf() that allows for regular expressions? 看到更多信息: 有JavaScript的String.indexOf()版本允许正则表达式吗?

"Check out this awesome video!".replace(/./g,function(match){
  return '0x'+match.charCodeAt(0).toString(16);
});

returns the hexadecimal representation of the string. 返回字符串的十六进制表示形式。 If you want just convert alpha-numeric characters, replace the . 如果只想转换字母数字字符,请替换. in the regex by \\w . \\w的正则表达式中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM