Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Need Help Combining Two Regular Expressions

Last post 01-13-2012, 12:36 PM by SimonDorfman. 2 replies.
Sort Posts: Previous Next
  •  01-12-2012, 12:00 PM 84498

    Need Help Combining Two Regular Expressions

    I'm working on a bookmarklet. I've got both parts working, but I don't know how to put them together.

    Here are two URLs to parse:

    http://bikes.trekbikes.co.jp/jp/ja/bikes/road/fitness/fx/7_3_fx/#/jp/ja/model/details?url=jp/ja/bikes/road/fitness/fx/7_3_fx
    http://bikes.trekbikes.com.cn/cn/zh/bikes/road/fitness/fx/7_3_fx/#/cn/zh/model/details?url=cn/zh/bikes/road/fitness/fx/7_3_fx

    I want a bookmarklet that will convert those two URLs into these answers:

    http://www.trekbikes.com/nl/nl/bikes/road/fitness/fx/7_3_fx/#/nl/nl/model/details?url=nl/nl/bikes/road/fitness/fx/7_3_fx
    http://www.trekbikes.com/nl/nl/bikes/road/fitness/fx/7_3_fx/#/nl/nl/model/details?url=nl/nl/bikes/road/fitness/fx/7_3_fx 

    Here is the first part of the answer:

    javascr1pt:(function() {window.location=window.location.toString().replace(/(bikes.trekbikes.co.jp|bikes.trekbikes.com.cn)/,'www.trekbikes.com');})()

    It replaces:

    bikes.trekbikes.co.jp
    or
    bikes.trekbikes.com.cn

    ...with:

    www.trekbikes.com

    Here is the second part of the answer:

    javascr1pt:(function() {window.location=window.location.toString().replace(/(jp\/ja\/|cn\/zh\/)/g,'nl/nl/');})() 

    It replaces all instances of:

    jp/ja/
    or
    cn/zh/

    ...with:

    nl/nl/ 

    How can I combine these two parts into one bookmarklet? Many thanks in advance for any help!  

    Filed under: ,
  •  01-12-2012, 5:17 PM 84499 in reply to 84498

    Re: Need Help Combining Two Regular Expressions

    I believe the answer is "you can't" simply because you have 2 different replacement strings.

    In general, regex engines can be really clever with that they can do during the pattern matching phase but are really very simplistic in how they do any replacements.

    Here you could quite easily combine the patterns so that it would find either of the sets of source strings (basically using the same alternation operator you have used already) possibly placing each into its own group so that you can tell which of the pair actually matched. However there is no syntax in any regex variant that I know of that lets you put in the equivalent to an "if" statement into the replacement string to let you select which string to use based on which matching group was actually used.

    You could use the type of pattern I suggested above and use the RegExp object's "exec()' function as this returns more information about the match. You could then examine this to determine which match group was used and then access the associated ".index" and ".length" parameters to do your own string substitutions. (If you do go this way, then be aware that each of your substitutions will alter the offsets for all subsequent matches - if you use the "global" option and therefore get back a collection of matches, you will need to work backwards through them so that the offsets remain valid).

    If possible, the simplest way might be to simply make 2 passes over the text, one for each replacement string.

    Susan

  •  01-13-2012, 12:36 PM 84505 in reply to 84499

    Re: Need Help Combining Two Regular Expressions

    Thanks, Susan.

    I figured out a way to do a double-pass with javascript, storing the first pass into a variable.

    javascr1pt:void((function(){var%20a=window.location.toString().replace(/(bikes.trekbikes.co.jp|bikes.trekbikes.com.cn)/,'www.trekbikes.com');window.location=a.replace(/(jp\/ja\/|cn\/zh\/)/g,'nl/nl/');})())

View as RSS news feed in XML