Monday, 11 April 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...