I don't understand the behaviour of Javascript Regex
Im validating a text with this code:
let validLine = /^-*[\w\s]+:(concept|term):-?(0[.]?\d*|1)$/g
let isValid = newText
.split('\n')
.reduce((valid, line) => {
line = line.trim()
if (line === '') return valid
return valid && !!validLine.exec(line)
}, true)
But this code does not behave as intended. The following, where I initialize the regex everytime does
let isValid = newText
.split('\n')
.reduce((valid, line) => {
let validLine = /^-*[\w\s]+:(concept|term):-?(0[.]?\d*|1)$/g
line = line.trim()
if (line === '') return valid
return valid && !!validLine.exec(line)
}, true)
Why?