replacing a varying substring with itself plus

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

    #1

    replacing a varying substring with itself plus

    Hi,
    I'd like to globally replace a substring specified by a regular
    expression with itself along with some additional characters.
    example:

    var oldString = 'asdf BRACKETTHISasdf sdf sd asdBRACKETTHISa sdf asd';

    var newString =
    oldString.repla ce(/\s\w*BRACKETTHI S\w*/g, '<' + ?some expression that
    accesses current match? + '>' )

    And newString winds up as:
    'asdf <BRACKETTHISasd fsdf> sd <asdBRACKETTHIS asdf>
    asd'

    Is there a way to do this, or do I have to use other methods?

    Thanks for helps,
    Kurt
  • Steve van Dongen

    #2
    Re: replacing a varying substring with itself plus

    thompsonsfatgaz elle@yahoo.com (kurt) wrote:
    [color=blue]
    >Hi,
    >I'd like to globally replace a substring specified by a regular
    >expression with itself along with some additional characters.
    >example:
    >
    >var oldString = 'asdf BRACKETTHISasdf sdf sd asdBRACKETTHISa sdf asd';
    >
    >var newString =
    >oldString.repl ace(/\s\w*BRACKETTHI S\w*/g, '<' + ?some expression that
    >accesses current match? + '>' )
    >
    >And newString winds up as:
    > 'asdf <BRACKETTHISasd fsdf> sd <asdBRACKETTHIS asdf>
    >asd'
    >
    >Is there a way to do this, or do I have to use other methods?
    >
    >Thanks for helps,
    >Kurt[/color]

    Add parentheses around the substring you care about and use $1 in the
    replacement text.
    .replace(/\s\w*(BRACKETTH IS)\w*/g, '<$1>' )

    http://msdn.microsoft.com/library/en...mthreplace.asp

    Regards,
    Steve

    Comment

    Working...