如何获得位于2个引号之间的子字符串? [英] How can I get a substring located between 2 quotes?
本文介绍了如何获得位于2个引号之间的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的字符串:您需要的单词是'hello'".
I have a string that looks like this: "the word you need is 'hello' ".
将"hello"(但不带引号)放入javascript变量的最佳方法是什么?我想办法是使用正则表达式(我对此了解甚少)?
What's the best way to put 'hello' (but without the quotes) into a javascript variable? I imagine that the way to do this is with regex (which I know very little about) ?
任何帮助表示赞赏!
推荐答案
使用 match()
:
> var s = "the word you need is 'hello' ";
> s.match(/'([^']+)'/)[1];
"hello"
这将匹配开始的'
,然后是除'
之外的所有内容,然后是结束的'
,将所有内容存储在 first 捕获组中.
This will match a starting '
, followed by anything except '
, and then the closing '
, storing everything in between in the first captured group.
这篇关于如何获得位于2个引号之间的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文