I need to include a "variable" in my regex that will be different each time it is called.

Very fustrated, this would have taken 2 seconds in Perl.

Here is my code:

for (y=0; y < response.length -1; y++){
                
                var _row = response[y].split(':');
                var str = "_row[1]";
                if(str.match("/^"+_inputText.value+"/i")){
                
                        _responseText += '<option value="'+_row[0]+'">'+_row[1]+', '+_row[2]+'</option>';
                }       
                }

I CANNOT get the variable to expand into the regex.
I have tried: everything I can think of...

re.test("/^"+_inputText.value+"/i")

This seemed to parse the variable into the regex, but I could never get it to match, or test positive.

var _regex = "/^"+_inputText.value+"/i";

Now _regex IS the string "/^ea/i", when passing ea as the inputtext.

But then, I can't get this to create a match:

var _regex = "/^"+_inputText.value+"/i";
                
                for (y=0; y < response.length -1; y++){
                
                var _row = response[y].split(':');
                var str = "_row[1]";
                if(str.match(_regex)){
                
                        _responseText += '<option value="'+_row[0]+'">'+_row[1]+', '+_row[2]+'</option>';
                }       
                }

???

Any help?

I just need a value supplied by the user to filter an array, weeding out those items that don't start with what the user typed.

Thanks!

Really my only problem is matching the beginning of line.
Because this works:

for (y=0; y < response.length -1; y++){
                
                var _row = response[y].split(':');

                if(_row[1].match(_inputText.value)){
                
                        _responseText += '<option value="'+_row[0]+'">'+_row[1]+', '+_row[2]+'</option>';
                }       
                }

But it matches anywhere in the string.
I need it to be true only if _inputText.value occurs at the beginning of the string.

:|

Ahhh!!

Hehehee...

Becoming obsessed with this...

Use the RegExp contructor. It accepts two arguments:
1. the regular expression pattern
2. reg ex options (g=global, etc)

var _regex = new RegExp( "^" + _inputText.value, "i");

NOTE: If your expression were:

var x = /^\d+$/ig

if you use the RegExp object you will need to escape the slash:

var x = new RegExp("^\\d+$","ig");

The same applies for the other shortcuts (\s, \w, etc)

Use the RegExp contructor. It accepts two arguments:
1. the regular expression pattern
2. reg ex options (g=global, etc)

var _regex = new RegExp( "^" + _inputText.value, "i");

NOTE: If your expression were:

var x = /^\d+$/ig

if you use the RegExp object you will need to escape the slash:

var x = new RegExp("^\\d+$","ig");

The same applies for the other shortcuts (\s, \w, etc)

Ahh Ha!!

Thank you!

I knew about the RegExe() object but, didn't know I could assemble the first argument like that.

Nice.

>>I knew about the RegExe() objec
It's RegEx() NOT RegExe()

>>Thank you!
You are welcome

Please can you help to use your example to make the following work. I'm not sure how to use RegExp in the code below. Much appreciated.

<html>
<head>
<script>
function test(matchit)
{
thedata="This is the hello world program";
thematch="/" + matchit + "/";  
r1= thedata.match(thematch);
if (r1 == null)
  alert("No Match")
else
  alert(r1[0]);
}
</script>
</head>
<body>
<script>test("hello");test("world")</script>
</body>
</html>

You actually don't need to use RegExp if you're using the match() method. Here's an example:

function test(matchit) {
	thedata = "This is the hello world program";
	r1 = thedata.match(matchit);
	if (r1 == null)
		alert("No Match")
	else
		alert(r1[0]);
}
This question has already been answered. Start a new discussion instead.