3

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.

5
  • 1
    It would probably be easier if you told us what it's supposed to match specifically ? Commented Jun 8, 2015 at 18:22
  • 1
    Please provide more information about your problem. What exactly do you mean by "use this ... in this string"? What is the expected outcome? Commented Jun 8, 2015 at 18:22
  • Are the quotes in your question literally part of the string? i.e., when you write " 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)? Commented Jun 8, 2015 at 18:24
  • I want match strings in a string. I mean I want to match those strings with a regular expression. I want to find anything between " and " but not \" . Commented Jun 8, 2015 at 18:26
  • Do you want to match empty string also like "" ? Commented Jun 8, 2015 at 18:34

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

6 Comments

This is probably closer to what he needs, but I don't think he realizes that. A faster way is "[^"\\]*(?:\\.[^"\\]*)*"
@sln So why use such complicated regex when "(.*)" can does the same?
@Kasra - Its not complicated, its the standard unrolled-loop method. Its 3-5 times faster than the alternation method.
thanks. and finally this is working even better: "(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'
Now test this solution with \"a\" "b"
|
1
[^\\]?(".*?[^\\]")

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.

https://regex101.com/r/nS2lT4/31

1 Comment

@moisrex did this fail in nay case?
0

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

Demo

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

Demo

As another way you can use a negative look-behind :

/(?<!\\)"(.*?)(?<!\\)"/

4 Comments

@moisrex Its negative look behind and will match string that not preced by pattern after ?<! read more regular-expressions.info/lookaround.html
unfortunately look-behind does not supports in JavaScript. but thanks for mentioning that. I learned a lot today.
Using (?<!\\)" 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.
@CasimiretHippolyte So in this case that you are talking about it can has a lot of scenarios. It's just a recipe for this case!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.