Regexp - global replace of a character between tags

Discussion in 'Javascript' started by MB, Mar 29, 2007.

  1. MB

    MB Guest

    I need to replace all occurances of a certain character located between
    two tags. I have included an example of what i have come up with so far,
    but it doesn't work they way I want it to:

    str = "some content<script type=\"text/javascript\">var str =
    'asdfasdfASDFASDF';<\/script>";
    str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim, "$1X$2");
    alert(str);

    In this example, i want all 'a' between the script tags to be replaced
    by 'X', but only the first is replaced. How can I modify this to replace
    *all* occurances of 'a'?

    /MB
     
    MB, Mar 29, 2007
    #1
    1. Advertising

  2. MB

    Evertjan. Guest

    MB wrote on 29 mrt 2007 in comp.lang.javascript:

    > I need to replace all occurances of a certain character located between
    > two tags. I have included an example of what i have come up with so far,
    > but it doesn't work they way I want it to:
    >
    > str = "some content<script type=\"text/javascript\">var str =
    > 'asdfasdfASDFASDF';<\/script>";
    > str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim, "$1X$2");
    > alert(str);
    >
    > In this example, i want all 'a' between the script tags to be replaced
    > by 'X', but only the first is replaced. How can I modify this to replace
    > *all* occurances of 'a'?
    >


    str = str.replace(/(<script.*?>)(.*)(?=<\/script>)/gi, function(x,y,z)
    {return y+z.replace(/a/gi,'Z')})

    [beware of line break. IE tested]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Mar 29, 2007
    #2
    1. Advertising

  3. MB

    MB Guest

    >> I need to replace all occurances of a certain character located between
    >> two tags. I have included an example of what i have come up with so far,
    >> but it doesn't work they way I want it to:
    >>
    >> str = "some content<script type=\"text/javascript\">var str =
    >> 'asdfasdfASDFASDF';<\/script>";
    >> str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim, "$1X$2");
    >> alert(str);
    >>
    >> In this example, i want all 'a' between the script tags to be replaced
    >> by 'X', but only the first is replaced. How can I modify this to replace
    >> *all* occurances of 'a'?
    >>

    >
    > str = str.replace(/(<script.*?>)(.*)(?=<\/script>)/gi, function(x,y,z)
    > {return y+z.replace(/a/gi,'Z')})
    >
    > [beware of line break. IE tested]
    >


    Thanks. This works well, however only as long as the contents of 'str'
    is just one line. It does not find any matches when I have several
    lines. I tried adding the m-flag to the regular expressions, but that
    didn't work. Can this be solved too?

    /MB
     
    MB, Mar 29, 2007
    #3
  4. MB

    Evertjan. Guest

    MB wrote on 29 mrt 2007 in comp.lang.javascript:

    >>> I need to replace all occurances of a certain character located
    >>> between two tags. I have included an example of what i have come up
    >>> with so far, but it doesn't work they way I want it to:
    >>>
    >>> str = "some content<script type=\"text/javascript\">var str =
    >>> 'asdfasdfASDFASDF';<\/script>";
    >>> str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim, "$1X$2");
    >>> alert(str);
    >>>
    >>> In this example, i want all 'a' between the script tags to be
    >>> replaced by 'X', but only the first is replaced. How can I modify
    >>> this to replace *all* occurances of 'a'?
    >>>

    >>
    >> str = str.replace(/(<script.*?>)(.*)(?=<\/script>)/gi,
    >> function(x,y,z) {return y+z.replace(/a/gi,'Z')})
    >>
    >> [beware of line break. IE tested]
    >>

    >
    > Thanks. This works well, however only as long as the contents of 'str'
    > is just one line. It does not find any matches when I have several
    > lines. I tried adding the m-flag to the regular expressions, but that
    > didn't work. Can this be solved too?



    str = str.replace(/\n/g,'\uffff').replace(/(<script.*?>)(.*)(?=
    <\/script>)/gi, function(x,y,z) {return y+z.replace(/a/gi,'Z')}).replace(/
    \uffff/g,'\n')

    [beware of line breaks. IE tested]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Mar 29, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Taylor

    RegExp between tags

    Charles Taylor, Apr 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,203
    Nergal
    Apr 8, 2005
  2. herman
    Replies:
    5
    Views:
    7,609
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
  3. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  4. replacing tags between tags

    , Sep 18, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    129
    J├╝rgen Exner
    Sep 19, 2005
  5. Replies:
    6
    Views:
    166
    Randy Webb
    Jan 27, 2006
Loading...

Share This Page