• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

I have two variables in Javascript. var1='Domestic violence life style' var2=''Domestic violence'. can any one provide me the code to split var1 by var2

I have two variables in Javascript. var1='Domestic violence life style' var2=''Domestic violence'. can any one provide me the code to split var1 by var2
0
ukapu2005
Asked:
ukapu2005
1 Solution
 
ZylochCommented:
Hi ukapu2005,

By split, do you mean split between 'Domestic violence' and 'life style' while keeping the Domestic violence text there?

To do that, you would do:

var someOddSep = '(#*@';
var1 = 'Domestic violence life style';
var2 = 'Domestic violence';

var pattern = new RegExp(var2);
tempVar = var1.replace(pattern, var2 + someOddSep);

var splitVars = tempVar.split(someOddSep);


If, on the other hand, you only want to separate by Domestic violence like you want to with a comma, by erasing the Domestic violence text, just do:

var splitVars = var1.split(var2);

Regards,
Ted
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now