问题描述
为什么我不能使用(?!y)apple,如果我想排除yapple?
Why can't I use (?!y)apple, if i want to exclude "yapple"?
什么是(?!y)的替代品之前一个词?
What is alternative to (?!y) before a word?
推荐答案
回答你的问题原始问题:
您可以明确地编码之前没有任何内容(字符串的开头)或非y字符
You can explecitely encode "preceeded by either nothing (beggining of string) or a non-y character"
/(?:^|[^y])apple/
或取决于你想做什么,你可以使用单词边界匹配
or depending on what you want to do, you can use a word-boundary match
/\bapple/
\ b
匹配字母数字字符(\ w)和非字母数字字符(\ W)之间的边界
\b
matches the boundary between an alphanumeric character (\w) and a non-alphanumeric character (\W)
其他环境(如Perl)有时会内置这种后置功能:
Other environments (like Perl) sometimes have this look-behind functionality conveniently builtin:
/(?=)/ positive lookahead
/(?!)/ negative lookahead
/(?<=)/ positive lookbehind
/(?<!)/ negative lookbehind
关于不替换内部内容的问题
如果你做了两个步骤,也许会更容易:第一步从字符串中删除< pre>
标记内的所有文字,然后执行替换,然后在完成后添加隐藏文本。
Perhaps it would be easier if you did a two step process: first remove all the text inside <pre>
tags from your string, then do the replacements and then add the hidden text back after you are done.
这篇关于替代(?!y)但在单词之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!