Match everything between 2 identifiers

Viewed 81

I am trying to match the phrase between identifier \ if the word NULL is present within.

Regex attempt:

/\\(.*?)\\/g

String:

id,name,age,score\n1,Jack,NULL,12\n17,Betty,28,11\cvdvdfv\dfsdfsdf

so far all the text between identifiers '' are selected if I try to fit the NULL word it doesn't do anything Regex attempt:

/\\(NULL)(.*?)\\/g

I would really appreciate any idea

1 Answers

You can use

/\\[^\\]*NULL[^\\]*\\/g

If you actually want to find a whole line between LF, newlines, with a string NULL, replace \\ with \n and [^\\] with [^\n]:

/\n.*NULL.*\n/g

Or, perhaps, you would just use /.*NULL.*/g.

Details

  • \\ - a backslash
  • [^\\]* - 0 or more chars other than a backslash
  • NULL - NULL string
  • [^\\]* - 0 or more chars other than a backslash
  • \\ - a backslash.

In this concrete case, you may use:

function solution(S) {
    return S.split('\n').filter(x => !/\bNULL\b/.test(x)).join('\n');
}
undefined
console.log(solution('header,header\nANNUL,ANNULLED\nnull,NILL\nNULL,NULL'))

Related