Anicho
Anicho

Reputation: 2667

split causing tail end to be lost

I am trying to keep the tail end of the split

myLink = link.split(/\d/, 2)[1]

However the string it is splitting

link = 1 some text 800 hello world

would get split like:

1,
some text

I end up losing the tail end 800 hello world.

What can I do to keep this information.

Upvotes: 1

Views: 248

Answers (5)

anuj arora
anuj arora

Reputation: 831

var link = '1 some text 800 hello world';

var myLink = link.match(/[A-Z\s]?[a-z\s]+|[0-9]+/g);

alert(myLink);

This will definitely lead you for what you want.

Upvotes: 1

anuj arora
anuj arora

Reputation: 831

var link = "1 some text 800 hello world";

var myLink = link.split(/\d+/,[0-9]);

alert(myLink[1]); //this will give you some text
alert(myLink[2]); //this will give you hello world

Upvotes: 0

davidethell
davidethell

Reputation: 12018

This is because you are passing a limit of 2 to the split function so you are only getting two results in your split array. Omit the 2 and you'll get them all:

var splitArray = link.split(/\d/);

Upvotes: 1

Srinivas B
Srinivas B

Reputation: 1852

Hi you can use,

        myLink = link.split(/\d/)[1];

Upvotes: 1

SergeS
SergeS

Reputation: 11779

Use look ahead in regexp

myLink = link.split(/(?=\d+)/, 2)[1]

Upvotes: 0

Related Questions