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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil

    #1

    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=userinpu t.value
    var sResult = sSomestring.rep lace(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

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

    On 14 Aug 2003 08:11:32 -0700, paulgiverny@hot mail.com (Phil) wrote:
    [color=blue]
    >I wonder how one converts a string into a real regex...
    >
    >if I try something like:
    >
    >var sRegex=userinpu t.value
    >var sResult = sSomestring.rep lace(sRegex, "xxx");
    >
    >the sRegex is interpreted as a string and not as a real /regex/[/color]

    new RegExp(sRegex)

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

    Comment

    • Martin Honnen

      #3
      Re: Javascript: How to convert userinput *string* to *regex* foruse in replace



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

      var rexExp = new RegExp(userinpu t.value)
      [color=blue]
      > var sResult = sSomestring.rep lace(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/

      Comment

      Working...