0

Not sure how valid is this approach, but I'm unable to split the string into 2 when there are repeated characters.

 var match = 's';
 var str = "message";
 var res = str.split(match, 2);

For instance i tried to use split() on the string "message", it results into:

me,""

So i did this:

 res = str.split(match, 3);

so now it resulted into:

me,,age

but as you can see im still missing the second 's' in the "message" string. what im trying to get is I'm passing a matched character (in above case var match which is dynamically generated) to the split() and splitting into 2. I was hoping to get something this:

res = me,,sage

is that possible using split() or is there a better method to achieve this?

P.S: in fiddle i've given another string eg: (string = "shadow") which works fine. Fails only when there are repeated letters in the string!

fiddle: https://jsfiddle.net/ukeeq656/

EDIT::::::::::::

Thanks everyone for helping me out on this...and so sorry for last min update on the input, i just realized that var match; could be a word too, as in var match = 'force'; and not just var match ='s'; where the string is "forceProduct", so when my match is more than just a letter, this approach works: str.split(match, 2);, but str.indexOf(match); doesnt obviously... could there be an approach to split: "","Product". My extreme apologies for not mentioning this earlier.any help on this would be appreciated!!

eg fiddle: https://jsfiddle.net/ukeeq656/3/

10
  • So you are happing to call something on "message" and get an array with 2 elements, one is "me" and the other is "sage"? Commented Aug 31, 2016 at 4:06
  • what is the desired output? [ 'me', 'age']? Commented Aug 31, 2016 at 4:11
  • @kcdragon: yes right~ Commented Aug 31, 2016 at 4:12
  • @ bryanmac: ideally the first part would be: 'me' and the second part would be 'sage', im getting 'me','age' instead.. Commented Aug 31, 2016 at 4:13
  • 1
    Please describe the precise behavior you are looking for. If the input contains a single "s", then you want to split on that, but not split on immediately following "s" characters? Or on any additional "s' characters anywhere in the string? What if there are three "s" characters in a row? Commented Aug 31, 2016 at 4:17

3 Answers 3

3

I don't think split() is the correct way to do this.

Please see below:

var match = 's';
var str = "message";
var index = str.indexOf(match);
var res =[];
res[0] = str.substring(0, index);
res[1] = " ";
res[2] = str.substring(index + 1);
console.log(res);
Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure what your end goal is but I think this gets you what you want.

var match = 's';
var str = "message";
var index = str.indexOf(match);
var res = str.substring(0, index) + ',' + str.substring(index + 1);
alert(res); // me,sage

Comments

1

You could write a function to do this;

function newSplit(str, match) {
  var num = str.indexOf(match);
  var res = [];
  res.push(str.substring(0, num));
  //res.push(str.substring(num + 1, str.length)); // this line has been modified
  res.push(str.substring(num + match.length, str.length));
  return res;
}
var match = 'force';
var str = 'forceProduct';
console.log(newSplit(str, match));

This is what you want?

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.