简体   繁体   English

仅当未跟随另一个特定字符串时才匹配字符串

[英]Match string only if not followed by another specific string

I have #[\\s]+on[az]+[\\s]*=#si . #[\\s]+on[az]+[\\s]*=#si#[\\s]+on[az]+[\\s]*=#si I need to match ( preg_replace ) string like onclick = but only if this string isn't followed by another specific string. 我需要匹配( preg_replace )字符串,如onclick =但仅当此字符串后面没有其他特定字符串时。

Example (bold is the match) 示例(粗体是匹配)

some text onClickOrWhatever = some text 一些文字onClickOrWhatever =一些文字

some text onClickOrWhatever = my special string some text 一些文字onClickOrWhatever =我的特殊字符串一些文字

Second line shouldn't be matched because the expression is follow by "my special string". 第二行不应该匹配,因为表达式后跟“我的特殊字符串”。 In another words, I need to keep intact specific inline javascript but remove(destroy) any other. 换句话说,我需要保持完整的特定内联JavaScript,但删除(销毁)任何其他。

This expression is a little too general. 这个表达有点过于笼统。 It would also partially match you told me once =) , I am aware of that. 它也会部分匹配you told me once =) ,我知道这一点。

Well to answer your question, you can use negative lookahead : 那么回答你的问题,你可以使用negative lookahead

#\s+on[a-z]+\s*=(?!\s*my special string)#i

You also don't need s flag here which is used for making DOT match newline . 你也不需要在这里用于制作DOT匹配newline s标志。

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

相关问题 匹配字符串,后跟另一个字符串 - Match string not followed by another string 正则表达式匹配一个数字后跟一个特定的字符串 - Regex to match a number followed by a specific string 使用preg_match匹配一个字符串,然后匹配另一个字符串 - using preg_match to match one string followed by another 正则表达式PHP-不匹配特定的字符串,后跟数字 - Regex PHP - dont match specific string followed by numeric REGEX:如何匹配后跟空格并包含特定字符/符号的字符串 - REGEX: How to match string that followed by space and contains specific char/symbol 正则表达式匹配未被另一个不同的特定字符串包围的特定字符串 - Regex to match specific string not enclosed by another, different specific string 如何在正则表达式中匹配字符串后跟重复模式? - How to match string followed by repeated pattern in regex? 如何在php中遍历字符串并替换仅后面跟着另一个“%”字符的“%”字符? - How do you loop through a string in php and replace '%' characters that are ONLY followed by another '%' character? 如何修改正则表达式以仅匹配字符串中的特定数字? - How to modify a regex to only match a specific number in a string? PHP 正则表达式匹配字符串中的所有单个字母后跟数字值 - PHP regex to match all single letters followed by numeric value in string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM