Monday 22 July 2019

How to pass a variable into the regex expression in javascript?

I'm making a list search tool to check for a specific name in a long list. I have been successful at hard coding the name into the regex, but I need it to be dynamic.
Here is the code:



    function processString(){
document.getElementById('textArea').value = "";
var inputString = document.getElementById('textBox').value;
var userIn = document.getElementById('userInput').value;
var regex = //use variable 'userIn' for regex
$('#textArea').html(result);


if(result != null){
for(var i = 0; i < result.length; i++){
document.getElementById('textArea').value += result[i] + '\r\n';
}
}

}


codepen

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...