I would like to get the text between double quotes using JavaScript. I found online something like title.match(/".*?"/);
but the thing is that sometimes I have text between double quotes but sometimes there are no quotes. What I am saying is that sometimes I receive strings like: Neque porro quisquam est qui dolorem ipsum
and sometimes strings like: Neque "porro quisquam est" qui dolorem ipsum
. The thing is, when I have text containing double quotes I want to retrieve the text between them but when they aren't present, I'd like the whole text.
Also I have observered that string.indexOf("\"")
does not work and I don't really know how to approach this problem. Thanks.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
try it with this one:
Debuggex Demo
In a single regex:
TEST:
Try:
Then
EDIT reworked to take into account both @AshishMaity comment in a discarded edit about matching more than one substring, and @JosephCho comment about the original breaking in case there is a single quote (str3 in the case above)
split() function can be used get value in double quote