Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何编写正则表达式来表示某些关键字,而在它们之后或之前没有任何字母/数字?(符号和空格是可选的)
我试着写这个,但它似乎不起作用:
let resWord = "[class|function|static|this|return]" let keyword = new Regex("[^[^a-zA-Z0-9]]"+resWord+"[^[a-zA-Z0-9]$]")
我是正则表达式的新手,所以如果这是一个愚蠢的问题,请原谅:)
这可以使用消极的前瞻性和落后性来实现
(?<![a-zA-Z0-9])(class|function|static|this|return)(?![a-zA-Z0-9])
或者没有他们。
([^a-zA-Z0-9])(class|function|static|this|return)([^a-zA-Z0-9])