简体   繁体   English

如何用正则表达式检查特殊字符?

[英]How to check special characters with Regular Expressions?

In javascript I check for some characters but I want to allow underscores and slashes but I don't know how. 在javascript中,我检查了一些字符,但我想允许使用下划线和斜杠,但我不知道该怎么做。

    alias: /^[a-z-Z0-9]{2,35}$/

How to put / and _ so it has not special meaning to Regexes. 如何放置/和_,所以对正则表达式没有特殊意义。

_ has no special meaning at all in Regex. _在正则表达式中根本没有特殊含义。

And if a character has special meaning, you can use \\ to "despecialize" it. 并且,如果字符具有特殊含义,则可以使用\\进行“非专业化”。

alias: /^[a-zA-Z0-9_\/]{2,35}/

(BTW, you can use \\w which means [a-zA-Z0-9_] , ie /^[\\w\\/]{2,35}/ . The \\ in \\w turns a normal character w to have a special meaning.) (顺便说一句,可以使用\\w这意味着[a-zA-Z0-9_]/^[\\w\\/]{2,35}/\\\\w转动一个正常字符w具有特殊含义。)

(Edit: Inside the […] the / will not be recognized as a delimiter so it is safe to use /^[\\w/]{2,35}/ . Thanks Andy E for showing this.) (编辑:在[…]内部, /不会被视为分隔符,因此可以安全使用/^[\\w/]{2,35}/ 。感谢Andy E展示了此内容。)

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

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