Javascript正则表达式查找不以“开头”的单词;我的回答是;

Javascript正则表达式查找不以“开头”的单词;我的回答是;,javascript,regex,Javascript,Regex,我正在尝试编写一个正则表达式,它将在大括号之间查找所有不以“my:”开头的值。例如,我想捕获{this},但不想捕获{my:monkey} 捕获所有内容的模式是: \{([^\}]*)\} 我很难让它工作。到目前为止,我最接近的镜头是: \{[^my:]*([^\}]*)\} 此操作失败,因为它只忽略以“m”、“y”或“:”开头的标记 我确信我忽略了一个命令,将“my:”视为一个区块 (注意:必须适用于Javascript)此选项应能: /\{((?!my:)[^}]+)\}/g 检查快速

我正在尝试编写一个正则表达式,它将在大括号之间查找所有不以“my:”开头的值。例如,我想捕获
{this}
,但不想捕获
{my:monkey}

捕获所有内容的模式是:

\{([^\}]*)\}
我很难让它工作。到目前为止,我最接近的镜头是:

\{[^my:]*([^\}]*)\}
此操作失败,因为它只忽略以“m”、“y”或“:”开头的标记

我确信我忽略了一个命令,将“my:”视为一个区块

(注意:必须适用于Javascript)

此选项应能:

/\{((?!my:)[^}]+)\}/g

检查快速演示

您可以执行以下操作:

var input = "I want to capture {this} but not {my:monkey}";
var output = input.replace(/{(my:)?([^}]*)}/g, function($0, $1, $2) { 
    return $1 ? $0 : "[MATCH]"; 
});
// I want to capture [MATCH] but not {my:monkey}

{(?!my:)(.*?}
在regex pal中工作:

总结如下:

// test match thing_done but not some_thing_done (using nagative lookbehind)
console.log(/(?<!some_)thing_done/.test("thing_done")); // true
console.log(/(?<!some_)thing_done/.test("some_thing_done")); // false

// test match thing_done but not think_done_now (using nagative lookahead)
console.log(/thing_done(?!_now)/.test("thing_done")); // true
console.log(/thing_done(?!_now)/.test("thing_done_now")); // false

// test match some_thing_done but not some_thing (using positive lookbehind)
console.log(/(?<=some_)thing_done/.test("thing_done")); // false
console.log(/(?<=some_)thing_done/.test("some_thing_done")); // true

// test match thing_done but not think_done_now (using positive lookahead)
console.log(/thing_done(?=_now)/.test("thing_done")); // false
console.log(/thing_done(?=_now)/.test("thing_done_now")); // true
//测试匹配已完成的事情,但未完成某些事情(使用nagative Lookback)
控制台日志(/(?)?
对话版本:

I need match some_thing_done not thing_done:
  Put `some_` in brace: (some_)thing_done
  Then put ask mark at start: (?some_)thing_done
  Then need to match before so add (<): (?<some_)thing_done
  Then need to equal so add (<): (?<=some_)thing_done
--> (?<=some_)thing_done
    ?<=some_: conditional back equal `some_` string
我需要匹配一些“完成的事情”和“未完成的事情”:
把‘some’
然后把问号放在开头:(?做了什么事?)

然后需要在添加之前进行匹配(您想用它做什么?只需测试字符串或进行某种替换?好主意,我应该想到它。
?和