简体   繁体   中英

Regex: get string between last character occurence before a comma

I need some help with Regex.

I have this string: \\\\lorem\\ipsum\\dolor,\\\\sit\\amet\\conseteteur,\\\\sadipscing\\elitr\\sed\\diam
and want to get the result: ["dolor", "conseteteur", "diam"]
So in words the word between the last backslash and a comma or the end.

I've already figured out a working test , but because of reasons it won't work in neither
Chrome (v44.0.2403.130) nor IE (v11.0.9600.17905) console.

There i'm getting the result: ["\\loremipsumdolor,", "\\sitametconseteteur,", "\\sadipscingelitrseddiam"]

Can you please tell me, why the online testers aren't working and how i can achieve the right result?

Thanks in advance.

PS: I've tested a few online regex testers with all the same result. (regex101.com, regexpal.com, debuggex.com, scriptular.com)

The string

'\\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam'

is getting escaped, if you try the following in the browser's console you'll see what happens:

var s = '\\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam'
console.log(s);
// prints '\loremipsumdolor,\sitametconseteteur,\sadipscingelitrseddiam'

To use your original string you have to add additional backslashes, otherwise it becomes a different one because it tries to escape anything followed by a single backslash.

The reason why it works in regexp testers is because they probably sanitize the input string to make sure it gets evaluated as-is .

Try this (added an extra \\ for each of them):

str = '\\\\lorem\\ipsum\\dolor,\\\\sit\\amet\\conseteteur,\\\\sadipscing\\elitr\\sed\\diam'

re = /\\([^\\]*)(?:,|$)/g

str.match(re)

// should output ["\dolor,", "\conseteteur,", "\diam"]

UPDATE

You can't prevent the interpreter from escaping backslashes in string literals , but this functionality is coming with EcmaScript6 as String.raw

s = String.raw`\\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam`

Remember to use backticks instead of single quotes with String.raw . It's working in latest Chrome, but I can't say for all other browsers, if they're moderately old, it probably isn't implemented.

Also, if you want to avoid matching the last backslash you need to:

  • remove the \\\\ at the start of your regexp
  • use + instead of * to avoid matching the line end (it will create an extra capture)
  • use a positive lookahead ?=

like this

s = String.raw`\\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam`;
re = /([^\\]+)(?=,|$)/g;
s.match(re);
// ["dolor", "conseteteur", "diam"]

You may try this,

string.match(/[^\\,]+(?=,|$)/gm);

DEMO

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM