#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0

    Replacing string if it is not inside an <a> tag


    Hi

    This is basically what i need:

    if i want to replace the string "test string" with "hello" in this original string:

    test string <a href="#a" onclick="myfunction('test string')">test string</a>


    i should get

    hello <a href="#a" onclick="myfunction('test string')">test string</a>


    my environment is asp.net c#

    thank you
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    That's not something you can do easily with a regular expression unless there are severe restrictions on the format of the original string (ex: the text to replace always occurs before the first <a> tag).

    If you can't place restrictions on the original string, then you should see if your application language has an HTML parser and use that instead.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by E-Oreo
    That's not something you can do easily with a regular expression unless there are severe restrictions on the format of the original string (ex: the text to replace always occurs before the first <a> tag).

    If you can't place restrictions on the original string, then you should see if your application language has an HTML parser and use that instead.
    ok so lets go a different way

    lets say i want to replace this text only if there are specific characters before and after the string (with unlimited amount).

    the characters are: ():,.!?

    something like this:

    replace "test string" with "hello":

    original: word1 word2 (test string!????) word3
    result: word1 word2 (hello!????) word3

    but nothing should change in the following cases:

    original: word1 word2 (atest string!????) word3
    result: word1 word2 (atest string!????) word3

    original: word1 word2 a(test string!????) word3
    result: word1 word2 a(test string!????) word3

    original: word1 word2 (test string!????)a word3
    result: word1 word2 (test string!????)a word3
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Yes that's possible. You could use something like this, if your regex engine supports positive lookbehind:
    Code:
    (?<=(^|\s)([():,.!?]+))test string(?=[():,.!?]+(\s|$))
    If it doesn't, you will have to match the first subgroup as part of the whole pattern and just make sure you include it in your replacement string using a back reference.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by E-Oreo
    Yes that's possible. You could use something like this, if your regex engine supports positive lookbehind:
    Code:
    (?<=(^|\s)([():,.!?]+))test string(?=[():,.!?]+(\s|$))
    If it doesn't, you will have to match the first subgroup as part of the whole pattern and just make sure you include it in your replacement string using a back reference.

    Thank you.

    working perfectly!

IMN logo majestic logo threadwatch logo seochat tools logo