You can create a new RegExp by instantiating a RegExp object like so:
var regex = new RegExp(userIn, "ig"); //use variable 'userIn' for regex
The first parameter is the value passed in from userInput, the second is the flags. i means case-insensitive, g means global (check the whole string, don't stop after finding the first match)
To use the regex to check the name against the list, use:
var match = inputString.match(regex);
This will return all the matches of regex in inputString
function processString() {
document.getElementById('textArea').value = "";
var inputString = document.getElementById('textBox').value;
var userIn = document.getElementById('userInput').value;
var regex = new RegExp(userIn, "ig"); //use variable 'userIn' for regex
var match = inputString.match(regex);
if (match !== null) {
for (var i = 0; i < match.length; i++) {
document.getElementById('textArea').value += match[i] + '\r\n';
}
}
}
<p>List of Names</p><input type="text" id='textBox' style='width:250px;'>
<br><br>
<p>name to search for</p><input type="text" id='userInput' style='width:250px;'>
<br><br>
<input type="button" value='search' style='width:250px;' onclick='processString()'>
<br><br>
<textarea name="area" id="textArea" cols="30" rows="10"></textarea>