Skip to content Skip to sidebar Skip to footer

How To Search For The Second Occurence Of ' Or " In Regex?

String combinations: str_search = adfa odf 'aso' str_search = do o sfo o'sfsdf' str_search = sdfosd'sf sd' What i've done so far: if( /\s*\S*['|']\s*\S*['|']$/.test(str_search) )

Solution 1:

Well, from your updates, it seems that you can use something a bit like this:

str = "adfa odf 'aso'";

if(/(?:'[^']+'|"[^"]+")$/.test(str)){
    res = str.replace(/(?:'[^']+'|"[^"]+")$/, "!string!");
    alert(res);
}

JSFiddle.


Solution 2:

Something like this will work to replace a string in quotes:

> "a 'asdf'".replace(/'[^']*'/, "replacement");
"a replacement"

In plain english: look for a quote, any number of non-quote characters and another quote and replace all of that with "replacement".


Post a Comment for "How To Search For The Second Occurence Of ' Or " In Regex?"