I have issues with matching a string with regexp in JS. I can use this:
/"[^"]*?"/g
in this string:
" this is a string "
but I can not use that in this:
" this is a \"string\" "
how can i fix that? thanks.
If i understand correct, what you want to do is test is a string is in correct format? so no premature string endings?
If that is the case you could use /"(?:[^"\\]|\\.)*"/g
"(.*)" can does the same?"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'\"a\" "b"[^\\]?(".*?[^\\]")
You can try something like this.You will need to grab the group or capture and not match.See demo.
https://regex101.com/r/nS2lT4/30
or
(?:[^\\]|^)(".*?[^\\]")
See demo.
In that case you shouldn't use [^"]* also you don't need none-greedy you can use following regex for matching all between 2 quote :
/"(.*)"/g
And if you want to match anything between " you can simply use a word character matcher with white-space matcher within a character class with a global modifier :
/[\w\s]+/g
As another way you can use a negative look-behind :
/(?<!\\)"(.*?)(?<!\\)"/
?<! read more regular-expressions.info/lookaround.html(?<!\\)" prevents quotes preceded by a backslash, but it does in this situation too: \\" where the quote is preceded by two (or an even number) of backslashes. In these cases the quote is no more escaped.
" this is a string ", are you expressing an 18-character or 20-character string? Are you using literal notation (where/starts an escape sequence) or the actual character-for-character values present in the string (where a/is just a/character)?""?