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?
You 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 of regex
\$
: Matches $
literal(Need to escape using \
)()
: Capturing group.*?
: Matches any stringYou 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);