I am calling JS .replace() method on the following text that is within a variable:
if firstVar == 'string'
alert firstVar
alert 'string'
console.log 'string multiple words'
console.log 'string multiple words, then variable' + secVar
console.log firstVar + 'variable, then string multiple words'
This is how the replace method goes:
textVariableReplaced = textVariable.replace(/(^.*[a-zA-Z0-9]) ([a-zA-Z\/\('"].*$)/gm, '$1($2)');
This is the regex (^.*[a-zA-Z0-9]) ([a-zA-Z\/\('"].*$)
.
This regex should take letter or number([a-zA-Z0-9]
), followed by a single space () (
), then any text till the end of the line, that starts with a letter or '
or "
or (
or /
([a-zA-Z\/\('"].*$
).
This operation gives the following result:
if(firstVar == 'string')
alert(firstVar)
alert('string')
console.log 'string multiple(words')
console.log 'string multiple words, then(variable' + secVar)
console.log firstVar + 'variable, then string multiple(words')
Everything is perfect, except for the last lines.
The replace function should not put parentheses around the part of the text that contains an odd amount of quotation marks ('
or "
). So that the three last lines looked like this:
console.log('string multiple words')
console.log('string multiple words, then variable' + secVar)
console.log(firstVar + 'variable, then string multiple words')
Edit:
I have found this link to find out, whether the amount of characters is even or odd: How do you match even numbers of letter or odd numbers of letter using regexp for mysql
Edit 2:
Using the provided example in the link above I am trying to put ('')+
within the regex.
But, I am not sure how to implement it right.