f



Javascript: How to convert userinput *string* to *regex* for use in replace

I wonder how one converts a string into a real regex...

if I try something like:

var sRegex=userinput.value
var sResult = sSomestring.replace(sRegex, "xxx");

the sRegex is interpreted as a string and not as a real /regex/

How can I change this so the user can enter a regex and have it interpreted?

I guess this requires some kind of cast but to what kind of objectype?
0
paulgiverny
8/14/2003 3:11:32 PM
comp.lang.javascript 38370 articles. 0 followers. javascript4 (1315) is leader. Post Follow

2 Replies
676 Views

Similar Articles

[PageSpeed] 20

On 14 Aug 2003 08:11:32 -0700, paulgiverny@hotmail.com (Phil) wrote:

>I wonder how one converts a string into a real regex...
>
>if I try something like:
>
>var sRegex=userinput.value
>var sResult = sSomestring.replace(sRegex, "xxx");
>
>the sRegex is interpreted as a string and not as a real /regex/

new RegExp(sRegex)

Jim.
-- 
comp.lang.javascript FAQ - http://jibbering.com/faq/

0
jim
8/14/2003 3:12:36 PM

Phil wrote:
> I wonder how one converts a string into a real regex...
> 
> if I try something like:
> 
> var sRegex=userinput.value

var rexExp = new RegExp(userinput.value)

> var sResult = sSomestring.replace(sRegex, "xxx");
> 
> the sRegex is interpreted as a string and not as a real /regex/
> 
> How can I change this so the user can enter a regex and have it interpreted?
> 
> I guess this requires some kind of cast but to what kind of objectype?


-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/

0
Martin
8/14/2003 3:14:48 PM
Reply: