2

I have this string :

Item 1Item 2Item 3Item 4

I want to get :

["Item 1", "Item 2", "Item 3", "Item 4"]

I tried to do this :

var string = 'Item 1Item 2Item 3Item 4'
var regex = string.split(/(\d)/)
regex.splice(-1, 1)
regex[regex.length - 2] += regex[regex.length - 1];
regex.splice(-1, 1)
console.log(regex);

But it doesn't work, any idea how to get the desired result?

EDIT :

The string at max could look like this :

Item 1Item 2Item 3Item 4Item NItem N-1
6
  • But I rather got : as in But I'd rather get:? Commented Jan 18, 2019 at 15:58
  • Is that the only string this needs to work for or are there more cases? Commented Jan 18, 2019 at 15:59
  • @treyBake sorry for the confusion - updated the question to be clearer. Commented Jan 18, 2019 at 15:59
  • Use string.split(/(?!^)(?=Item)/) Commented Jan 18, 2019 at 15:59
  • @JamesCoyle updated the answer to add another example. Commented Jan 18, 2019 at 16:00

4 Answers 4

10

Note: the answer handles the original case - before the update

Use String.match() to find a sequence of non numbers that ends with a sequence of numbers (regex101):

var string = 'Item 1Item 2Item 3Item 4'
var arr = string.match(/\D+\d+/g)
console.log(arr);

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

7 Comments

\d -> \d+ is better
And if the string looks like this Item 1Item 2Item 3Item 4Item NItem N-1?
@IslamElshobokshy just to be clear, do you actually have "Item N" or "Item N-1" in your string, or is "N" and "N-1" just a placeholder for an arbitrary sequence of digits. (You need to use very precise language when discussing regexes).
@p.s.w.g they're actually strings or I would have mentioned it, but you're absolutely right I needed to clarify.
I've seen it, but the other example I gave have the string Item NItem N-1, and they're not numbers ^^ So can I use /\D+|N+|N\-1+/g?
|
5

When you have a capturing group ((...)) in the regex you pass to .split, the captured text is returned as part of the result. This is why you have the "1", "2", etc. in between the "Item "'s.

Apart from some variation of @Ori Dori's solution, you could also solve this with a lookahead assertion (?=...), which is a non-capturing construct.

var string = 'Item 1Item 2Item 3Item 4Item NItem N-1';
var arr = string.split(/(?=Item)/);
console.log(arr);

This will work by breaking apart the string immediately before any appearance of the text "Item".


For a more brute-force solution without using regex at all:

var string = 'Item 1Item 2Item 3Item 4Item NItem N-1';
var arr = [];
var i = string.indexOf('Item', 1);
while (i >= 0) {
  arr.push(string.substr(0, i));
  string = string.substr(i);
  i = string.indexOf('Item', 1);
}
arr.push(string);
console.log(arr);

Comments

3

If your elements always start with the word Item you can do something like this using split():

const str = 'Item 1Item 2Item 3Item 4Item N-2Item N-1Item N'
var arr = str.split("I").slice(1).map(x => "I" + x);
console.log(arr);

Even better, and alternatively you can do it with a single regular expression using match()

const str = 'Item 1Item 2Item 3Item 4Item 999Item N-2Item N-1Item N'
var arr = str.match(/Item\s[1-9|N|-]+/g);
console.log(arr);

Comments

0

To achieve expected result, use below option with replace, split and slice

  1. Add double space before 'I' using replace
  2. Split by double space using split(' ')
  3. remove first empty string using slice(1)

var str = "Item 1Item 2Item 3Item 4Item NItem N-1"

let arr = str.replace(/\I/g, '  I').split('  ').slice(1);

console.log(arr)

codepen - https://codepen.io/nagasai/pen/qLzKwJ?editors=1010

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.