Monday, July 22, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...