I am trying to match anything except between ' quotes '. If possible between '
"
`
quotes (I know I can use ['"`]
).
Here is my regex pattern which just gets all text in between '
"
`
.
^((?!\'.*\').*)$
Note: I am talking about JavaScript Regex, therefor, I don't need PHP or Python regex patterns.
let string = 'lorem \\'ipsum\\' dolor' let match = string.match(/^((?!\\'.*\\').*)$/) console.log('[===Real output===]') console.log(match[1]) console.log('[===Expected output===]') console.log('lorem dolor')
Match the " ' ** and then use that match to find the other one, after matching any characters besides the one of the **" '
that we just found
string.replace(/(['"`])(?:(?!\1)[\s\S])+\1/g, '')
If you want it to match no characters in between marks, as well, then:
string.replace(/(['"`]).*?\1/g, '')
To separate the quotes by type so they do not get mixed (ex. "words'
), \\B
: Non-word Boundary meta sequence was wrapped around each QUOTE .+?
QUOTE and then each of those expressions separated by |
: OR.
/(\B(".+?")\B|\B('.+?')\B|\B(`.+?`)\B)/g;
Alternatively, QUOTE .*?
QUOTE could be used if you expect empty quotes as well.
var rgx = /(\\B(".+?")\\B|\\B('.+?')\\B|\\B(`.+?`)\\B)/g; var str = `"Lorem ipsum dolor sit amet", consectetur adipisicing elit, "'sed do eiusmod tempor,'" incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, \\`quis nostrud\\` exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \\`Duis aute irure dolor\\` in "reprehenderit in voluptate" velit esse cillum 'dolore eu fugiat nulla' pariatur. `; var sub = ``; var res = str.replace(rgx, sub); console.log(res);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.