简体   繁体   English

在此示例中,Regexp在white-space之后匹配

[英]Regexp matches after white-space on this example

/.n/g matches "nope, an apple is on the tree" /.n/g匹配“没了, 一个苹果树”

Notice that the ' n ' of ' nope ' is unaffected because: 请注意,“ nope ”的“ n ”不受影响,因为:

(The decimal point) matches any single character except the
newline characters: \n \r \u2028 or \u2029. ([\s\S] can be
used to match any character including newlines.)

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp

.

But the problem is that I do NOT want to match the 'n' that comes after a white-space: 但问题是我不想匹配白色空间后面的'n':

/.n/g matches "I don't want to match this n !" /.n/g匹配“我不想匹配此n !”

How can I achieve this? 我怎样才能做到这一点?

A regexp like /[\\S]n/g (or the shorter, less readable, version /\\Sn/g will provide you with the functionality you asked for. \\S will match any character that isn't a whitespace. /[\\S]n/g表达式(或更短,可读性较低的版本/\\Sn/g将为您提供所要求的功能。 \\S将匹配任何不是空格的字符。

Remember that \\S will not match " " , "\\t" , etc. if you just want to ignore "real" spaces /[^ ]n/g is the way to go. 请记住, \\S将不匹配" ""\\t"等,如果您只想忽略“实”空格/[^ ]n/g/[^ ]n/g的方法。

Although javascript regexps derive from Perl, they do not implement conditional matching and lookarounds. 尽管javascript正则表达式派生自Perl,但它们不实现条件匹配和环顾四周。 I believe, your only option is to exclude the whitespace family as @refp and @TudorConstantin have already suggested: /\\Sn/g . 我相信,你唯一的选择是排除空格族,因为@refp和@TudorConstantin已经建议: /\\Sn/g

请尝试使用排除空格族的字符类:

/[\S]n/g

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

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