admin管理员组文章数量:1391975
i need to find the first occurrence of string between two string in Javascript, this is an example of my string:
"$$ hi my name is Mark $$"
i want get the text between the $$ how can i do that?
i need to find the first occurrence of string between two string in Javascript, this is an example of my string:
"$$ hi my name is Mark $$"
i want get the text between the $$ how can i do that?
Share Improve this question edited Jul 9, 2015 at 9:10 Tushar 87.3k21 gold badges163 silver badges181 bronze badges asked Jul 9, 2015 at 9:04 PieroPiero 9,27321 gold badges93 silver badges162 bronze badges3 Answers
Reset to default 5You can use following regex
var myStr = "$$ hi my name is Mark $$ And his name is John $$";
var matches = myStr.match(/\$\$(.*?)\$\$/);
var str = matches && matches.length ? matches[1] : '';
alert(str);
Regex Explanation
/
: Delimiter ofregex
\$
: Matches$
literal(Need to escape using\
)()
: Capturing group.*?
: Matches any string
You can use a regular expression :
var mys = /\$\$(.*)\$\$/.exec('$$ hi my name is Mark $$')[1]
You can do this with regular expressions. As you only want the first match make sure to use non greedy.
var yourVariable = "$$ hi my name is Mark $$ more stuff $$";
var match = yourVariable.match(/\$\$(.*?)\$\$/)[1];
alert(match);
本文标签: Get first occurrence of string between two string in JavascriptStack Overflow
版权声明:本文标题:Get first occurrence of string between two string in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744763552a2623894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论