简体   繁体   中英

Regex how to match exact string pattern

I have a long string like this:

['2021_04_27__21_52_09', '2021_04_27__21_52_13', '2021_04_27__21_52_16']

I'm trying to convert the string into an array by using string.split method. To split properly, I'd have to remove:

  1. [
  2. ]
  3. '
  4. (comma)
  5. (space)

I have tried using the regular expression [&#39$;?,? ] [&#39$;?,? ] but this removes all 3s and 9s (for example the 3 in 2021_04_27__21_52_13). I want to remove 3s and 9s only in the sequence &#39$; .

What is the proper regular expression?

You can use

(?:'|[\][,\s])+

See the regex demo . Details :

  • (?: - a non-capturing group matching
    • ' - a ' substring
    • | - or
    • [\][,\s] - a ] , [ , comma or whitespace
  • )+ - one or more times.

See the JavaScript demo:

 const str = "['2021_04_27__21_52_09', '2021_04_27__21_52_13', '2021_04_27__21_52_16']"; console.log(str.split(/(?:'|[\][,\s])+/).filter(Boolean)) // => ["2021_04_27__21_52_09", "2021_04_27__21_52_13", "2021_04_27__21_52_16"]

Note that .filter(Boolean) removes empty elements from the resulting array.

I would gravitate towards doing a regex find all approach here, using match() , rather than trying to split:

 var input = "['2021_04_27__21_52_09', '2021_04_27__21_52_13', '2021_04_27__21_52_16']"; var matches = input.match(/\d{4}_\d{2}_\d{2}__\d{2}_\d{2}_\d{2}/g); console.log(matches);

One way to do that is by cleaning up the characters in multiple steps, then splitting by ; .

 const str = "['2021_04_27__21_52_09', '2021_04_27__21_52_13', '2021_04_27__21_52_16']"; const clean = str.replace(/[\[\]\s\,]/g,"") // removes [] space and, .replace(/&#39/g,"") // removes &#39.split(/;/) // splits on; .filter(e => e.length > 0); // removes any empty strings console.log(clean);

To convert that string to an array, I would replace the ' - which is an encoded single quote, char(39) - with double quote characters. Based on the example you provided this gives us valid JSON, and then we can use JSON.parse() , as follows:

 const str = "['2021_04_27__21_52_09', '2021_04_27__21_52_13', '2021_04_27__21_52_16']"; const str2 = str.replace(/'/g, '"') const arr = JSON.parse(str2); console.log(arr);

The idea is to keep the regex as simple as possible, without having to provision for what else could be in the string. All it does is "make the quotes usable".

And then we parse the result using the simplest way there is.

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