用于捕获括号中单词的正则表达式
RegEx for capturing words in parentheses
这是我的正则表达式:
/(\(-caramel-\)|\(-vanilla-\)|)/
使用此正则表达式,这将仅检测 first string 仅 (-caramel-)
如何检测字符串中的所有 (-caramel-) 和 (-vanilla-)?
这是我的示例字符串:
(-caramel-) John Cina (-vanilla-)(-caramel-)2019-05-19 07:31:05(-vanilla-)
我该如何解决这个问题?
如果我们希望在搜索中出现拼写错误的单词,我们可以使用字符列表来做到这一点,从左到右用 ()
分隔,可能类似于:
\([carmel-]+\)|\([vanila-]+\)
DEMO
正则表达式
如果不需要此表达式,可以在 regex101.com 中对其进行修改或更改。
正则表达式电路
jex.im 可视化正则表达式:
测试
$re = '/\([carmel-]+\)|\([vanila-]+\)/m';
$str = '(-caramel-) John Cina (-vanilla-)(-caramel-)2019-05-19 07:31:05(-vannila-)';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
演示
此代码段仅显示如果表达式有效:
const regex = /\([carmel-]+\)|\([vanila-]+\)/gm;
const str = `(-caramel-) John Cina (-vanilla-)(-caramel-)2019-05-19 07:31:05(-vannila-)`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
这是我的正则表达式:
/(\(-caramel-\)|\(-vanilla-\)|)/
使用此正则表达式,这将仅检测 first string 仅 (-caramel-)
如何检测字符串中的所有 (-caramel-) 和 (-vanilla-)?
这是我的示例字符串:
(-caramel-) John Cina (-vanilla-)(-caramel-)2019-05-19 07:31:05(-vanilla-)
我该如何解决这个问题?
如果我们希望在搜索中出现拼写错误的单词,我们可以使用字符列表来做到这一点,从左到右用 ()
分隔,可能类似于:
\([carmel-]+\)|\([vanila-]+\)
DEMO
正则表达式
如果不需要此表达式,可以在 regex101.com 中对其进行修改或更改。
正则表达式电路
jex.im 可视化正则表达式:
测试
$re = '/\([carmel-]+\)|\([vanila-]+\)/m';
$str = '(-caramel-) John Cina (-vanilla-)(-caramel-)2019-05-19 07:31:05(-vannila-)';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
演示
此代码段仅显示如果表达式有效:
const regex = /\([carmel-]+\)|\([vanila-]+\)/gm;
const str = `(-caramel-) John Cina (-vanilla-)(-caramel-)2019-05-19 07:31:05(-vannila-)`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}