jQuery 获取括号之间的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18377113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get text between brackets
提问by Mamadou
I have this string : Charles de Gaulle, (Paris) [CDG]
我有这个字符串: Charles de Gaulle, (Paris) [CDG]
I would like in JavaScript/jQuery get just Paris
. The initial string can have variable length.
我想在 JavaScript/jQuery 中得到Paris
. 初始字符串可以具有可变长度。
I have tried this:
我试过这个:
var tab = "Charles de Gaulle, (Paris) [CDG]";
var tab2 = tab.split(',');
var tab3 = tab2.split('[')
回答by Suresh Atta
回答by Tushar
here is my code
这是我的代码
"This is (my) text".match(/\(([^)]+)\)/)[1]
The match() returns an array ["(my)","my"] from which the second element is extracted.
match() 返回一个数组 ["(my)","my"],从中提取第二个元素。
回答by Prats
You can use .slice(begin,end)
instead of .substring()
.
您可以使用.slice(begin,end)
代替.substring()
.
For example click on the link to view your result: http://jsfiddle.net/qhZq5/
例如点击链接查看你的结果:http: //jsfiddle.net/qhZq5/
回答by Alessandro Minoccheri
try this:
尝试这个:
var str = "Charles de Gaulle, (Paris) [CDG]",
pos = str.indexOf("(") + 1;
str = str.slice(pos, str.lastIndexOf(")"));