2種類の区切り文字(口ひげテンプレートと同様)を持つトークンの文字列を解析しています。
一致する純粋な正規表現ソリューションが必要です {{bob}}
に this is {{bob}} a double token
。しかし、一致しません this is {{{bob}}} a triple token
私はダブルと一致しています
{{[^{]([sS]+?)[^}]}}
しかし、それは一致します {{bob}}
トリプル内 {{{bob}}}
.
ネガティブな見方がなければ、純粋な正規表現の解決策を見つけるのに苦労しています。何かアドバイスはありますか?
回答:
回答№1は0角かっこ以外の空白文字は、次のように検索できます。
s({{([^{}]+?)}})s
回答№2の場合は0
RegEx:
^(}?){{[^{]([sS]+?)[^}]}}
自動生成された説明:
^ asserts position at start of the string
1st Capturing Group (}?)
}? matches the character } literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
{ matches the character { literally (case sensitive)
{ matches the character { literally (case sensitive)
Match a single character not present in the list below [^{]
{ matches the character { literally (case sensitive)
2nd Capturing Group ([sS]+?)
Match a single character present in the list below [sS]+?
+? Quantifier — Matches between one and unlimited times, as few times as possible, expanding as needed (lazy)
s matches any whitespace character (equal to [rntfv ])
S matches any non-whitespace character (equal to [^rntf ])
Match a single character not present in the list below [^}]
} matches the character } literally (case sensitive)
} matches the character } literally (case sensitive)
} matches the character } literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don"t return after first match)
回答№3の場合は0
以下を使用して、一致を抽出できます。
var s = "this is {{{ bad bob triple}}} a triple token {{bob double}} {{bob}} a double token {{{bad token}} {{bad token}}}";
var rx = /(?:^|[^{]){{([^{}]*)}}(?!})/g;
var m, res=[];
while(m=rx.exec(s)) {
res.push(m[1]);
}
console.log(res);
を参照してください 正規表現のデモはこちら.
(?:^|[^{])
-文字列の先頭または任意の文字ですが{
{{
-ダブル{
([^{}]*)
-グループ1:任意の文字{
そして}
ゼロ回以上}}
-ダブル}
(?!})
-すぐには続かない}
.