Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Phil
Guest
 
Posts: n/a
#1: Jul 20 '05
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?

Jim Ley
Guest
 
Posts: n/a
#2: Jul 20 '05

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


On 14 Aug 2003 08:11:32 -0700, paulgiverny@hotmail.com (Phil) wrote:
[color=blue]
>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/[/color]

new RegExp(sRegex)

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

Martin Honnen
Guest
 
Posts: n/a
#3: Jul 20 '05

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




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

var rexExp = new RegExp(userinput.value)
[color=blue]
> 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?[/color]


--

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

Closed Thread