在两个圆括号之间获取文本 [英] Get text between two rounded brackets
本文介绍了在两个圆括号之间获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用JavaScript中的正则表达式从下一句中的两个圆括号中检索单词 my
?
这是(我的)简单文本
解决方案
< pre class =snippet-code-js lang-js prettyprint-override>
console.log(这是(我的)简单文本.match(/ \(([^]] +) \)/)[1]);
\(
正在打开大括号,(
- 子表达式的开头, [^]] +
- 除了右括号一次或多次之外的任何事情(你可能想要用 *
替换 +
), )
- 子表达式结束, \)
- 结束括号。 match()
返回一个数组 [(my),my]
,其中第二个元素是提取。
How can I retrieve the word my
from between the two rounded brackets in the following sentence using a regex in JavaScript?
"This is (my) simple text"
解决方案
console.log(
"This is (my) simple text".match(/\(([^)]+)\)/)[1]
);
\(
being opening brace, (
— start of subexpression, [^)]+
— anything but closing parenthesis one or more times (you may want to replace +
with *
), )
— end of subexpression, \)
— closing brace. The match()
returns an array ["(my)","my"]
from which the second element is extracted.
这篇关于在两个圆括号之间获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文