Home > Back-end >  Regex: matches to new line or end of line or space in JS
Regex: matches to new line or end of line or space in JS

Time:12-22

I wanted to extract :privacy and :date from the example string below.

I wanted to have a regex constraint that describes that a :[^:\s] block (e.g. :privacy or :date) can only be ended by a space \s or a newline \n or a end of string $ (so I will be able to have a rule to logically split these blocks in the later steps).

So I simply put (?:$|\n|\s) at the end of the regex, but I doesn't work for me (the 3rd regex below). I double checked that it does work when I separately put \s or $ (the 1st and 2nd regex below), now I have no idea how I can implement the thing. Thanks for your help.

'note::tmp hogehoge. :privacy :date'.match(/\s:[^:\s] \s/g)
(1) [' :privacy ']

'note::tmp hogehoge. :privacy :date'.match(/\s:[^:\s] $/g)
(1) [' :date']

'note::tmp hogehoge. :privacy :date'.match(/\s:[^:\s] (?:$|\n|\s)/g)
(1) [' :privacy ']

CodePudding user response:

You can use below regex pattern to match a block pattern :[^:\s ] ended by a space \s or a newline \n or a end of string $

/((:[^:\s ] )(?:[\s\n]))|(:[^:\s ] )(?:[\s\n])?$/gm

(?:[\s\n]) - will check if the block is being followed by a space or a new line
(:[^:\s ] )(?:[\s\n])?$ - will check if the block is at the end of string or not.

you can also use lookforward technique to achieve the same result

(:[^:\s ] )(?=\s|\n|$)

CodePudding user response:

In your pattern \s:[^:\s] \s you are matching the leading and the trailing whitespace chars.

What you might do is assert a whitespace boundary using (?!\S) to the right.

To get the value without the leading whitespace char, you can use a capture group.

\s(:[^:\s] )(?!\S)

Regex demo

const s = "note::tmp hogehoge. :privacy :date";
const regex = /\s(:[^:\s] )(?!\S)/g;
const result = Array.from(s.matchAll(regex), m => m[1]);
console.log(result);

  • Related