0

I want to use Regular expression in Javascript split() method

Target String to run a split method against is:

type=Search&searchType=simple&searchField=partOne && partTwo&highlight=partOne&isAllProductsContext=true&isAllProductInstance=false&contextId=&effectivity=

My code:

this.regexPattern = new RegExp("((?<=&&)&(?!&))|((?<!&)&(?!&))",'ig');
var keysAndValuesArray = targetString.split(regexPattern);
5
  • My code --> this.regexPattern = new RegExp("((?<=&&)&(?!&))|((?<!&)&(?!&))",'ig'); var keysAndValuesArray = targetString.split(regexPattern); Commented Oct 24, 2013 at 11:27
  • Can you tell us the expected output? Commented Oct 24, 2013 at 11:33
  • Problem - I want to split following String Target String: type=Search&searchType=simple&searchField=piston && rod&highlight=piston into array of string as: 1.type=Search 2.searchType=simple 3.searchField=piston && rod 4.highlight=piston i.e I want to split on '&' in target string, BUT NOT ON '&&' (&& is operator in my string) Can you please suggest how to do that in javascript ? Commented Oct 24, 2013 at 11:37
  • If you are going to use this in URL, I would suggest URL Component encoding piston && rod. Commented Oct 24, 2013 at 11:39
  • Not really - I'm not using this in URL, I'm using this as string passed to Server in form of Object. Commented Oct 24, 2013 at 11:55

4 Answers 4

1

Javascript regex engine doesn't support lookbehind as in your regex.

You will need to rewrite your regex to be used in Javascript. If you define your problem clearly we can give you some alternatives.

UPDATE Here is Javascript code that can work with OP's requirements:

s = 'type=Search&searchType=simple&searchField=partOne && partTwo&highlight=partOne&isAllProductsContext=true&isAllProductInstance=false&contextId=&effectivity=';
m  = s.match(/[^&]+(?:&&[^&]*)?(?=&|$)/g);

**OUTPUT:

["type=Search", "searchType=simple", "searchField=partOne && partTwo", "highlight=partOne", "isAllProductsContext=true", "isAllProductInstance=false", "contextId=", "effectivity="]
Sign up to request clarification or add additional context in comments.

8 Comments

I want to split targetString as - type=Search & searchType=simple &searchField=piston && rod &highlight=piston what could be done then ? Need change in Regular Expression ? as this same regex worked fine in my JAVA code but not working in Javscript code. Pls advise
Because Java regex engine has support of lookbehind but Javascript doesn't support that.
Problem - I want to split following String Target String: type=Search&searchType=simple&searchField=piston && rod&highlight=piston into array of string as: 1.type=Search 2.searchType=simple 3.searchField=piston && rod 4.highlight=piston i.e I want to split on '&' in target string, BUT NOT ON '&&' (&& is operator in my string) Can you please suggest how to do that in javascript using regex ?
Not match, I want to split. Can this same expression will work for split() ? I'm not sure that it will.
End result is always an array of strings you are interested in. Looks like you have accepted pretty complex solution for this simple probem, do you still want a modified solution?
|
0

Not split, but match does a similar work

var test = 'type=Search&searchType=simple&searchField=partOne && partTwo&highlight=partOne&isAllProductsContext=true&isAllProductInstance=false&contextId=&effectivity=';
var re = /(?:[a-zA-Z]+=(?:(?:[^&]+(?:&&)?)+)(?=&|$))/g;
alert( test.match(re) );

Comments

0
targetString.replace(/([^&])&([^&])/g,"$1&&&$2")
            .split("&&&");

or match it incase &&& doesn't work for you

var match,results = [];
var patt=/(.*?[^&])&(?=[^&])/g;
while (match = patt.exec(str))
   results.push(+match[1]);

3 Comments

This will replace any '#' coming in targetString as well. Not a good idea.
No Can't take this risk of replacing characters, I'm not sure what user can enter in the form of TargetString.
if TargetString contains &&& then also it gives problem.
0

You could try this :

var keysAndValuesArray = targetString.match(/(&\W+|[^&])+/g);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.