0

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!

5
Contributors
10
Replies
14
Views
8 Years
Discussion Span
Last Post by itsjareds
0

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...

0

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)

0

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.

0

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

>>Thank you!
You are welcome

0

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>
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.