2

Below is an example of a String that I want to split it to 3 parts from a substring

test01String001s

Examples:

  • example_1 subString == "0"

    res: [ "test","0","1String001s" ] - test01String001s

  • example_2 subString == "st"

    res: [ "te","st","01String001s" ] - test01String001s

  • example_3 subString == "01s"

    res: [ "test01String0","01s", "" ] - test01String001s

  • example_4 subString == "t"

    res: [ "","t","est01String001s", ] - test01String001s

I try to resolve it via

 var string='test01String001s'
 var keyword="01"
 console.log(string.split(keyword));

output:

     ["test","String0","s"]  instead of [ "test","01","String001s"]

or

 var string='test01String001s'
 var keyword="0"
 console.log(string .split(keyword, 2));

output

 ["test","1String"]
3
  • Does this answer your question? Split string once in javascript? Commented Oct 6, 2021 at 14:27
  • Actually not completely. I just add a answer in a comment but I think that we are close Commented Oct 6, 2021 at 14:34
  • To keep the separator, you can use [s.slice(0,i), separator, s.slice(i+1)] Commented Oct 6, 2021 at 14:49

4 Answers 4

1

To get 3 parts I wouldn't use split, but use some logic

const mystring = 'test01String001s'
const sp = ['01','1','t','st', '1s']

sp.forEach(item=>{
  let idx=mystring.indexOf(item)
  let result=[]
  if(idx===0){
    result.push('')
    result.push(item)
    result.push(mystring.slice(item.length))
  }else if (idx+item.length ==mystring.length){
    result.push(mystring.slice(0,idx))
    result.push(item)
    result.push('')
  }else if(idx!=-1){
    result.push(mystring.slice(0,idx))
    result.push(item)
    result.push(mystring.slice(idx+item.length))
  }
  console.log(result)
})

Sign up to request clarification or add additional context in comments.

Comments

1

Try this.

var string = "test01String001s";
var splitKey = '01';
var components = string.split(splitKey);
var array = ([components.shift(), components.join(splitKey)]);
array.splice(1, 0, splitKey);
console.log(array.join());

Output: test,01,String001s

1 Comment

@NakosKotsanis Happy to help you. If it helps you, Please accept it
0

This should work:

const test = 'test01String001s';
const key = "t";
const re = new RegExp('(.*?)(' + key + ')(.*)');
const result = test.match(re, "regex");
result.shift();

console.log(result);

So this regex match is returning an array of:

(.*?) - anything before key

(' + key + ') - key

(.*) - anything after key

Unfortunately it also returns the whole matched word, so I used shift to remove first match from array.

Comments

0

I have written one function named split. It will work fine if splitter is found in a string, otherwise it will return [string, splitter, string]

For example:

string = 'test01String001s and splitter=01 now give output ["test", "0", "1String001s"]

if splitter = "apple" it will return ["test01String001s", "apple", "test01String001s"]

If you want to return empty value when splitter not found in a string, simply add if(splitter_index < 0) return ["", "", ""] before current return statement line in split function.

Please check all examples below and anyone think any changes needed please comment down.

let string = "test01String001s"

function split(string, splitter) {
    let splitter_index = string.indexOf(splitter);
    
    return [string.slice(0, splitter_index), splitter, string.slice(splitter_index+splitter.length)];
}

// example_1
console.log(split(string, '0'));

// example_2
console.log(split(string, 'st'));

// example_3
console.log(split(string, '01s'));

// example_4
console.log(split(string, 't'));

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.