1

If i have:

var x = "123442234535asd";

What regular expression would I have to use on x.split(regex); to get "asd"?

PS: x would always have a random string of numbers first and a string of letters after.

2
  • 3
    And why not just remove the numbers, x.replace(/\d/g,''); Commented Apr 18, 2015 at 22:38
  • 2
    Couldn't you just match [a-zA-Z]+? Commented Apr 18, 2015 at 22:38

3 Answers 3

2

You can simply remove the digits:

var x = "123442234535asd";
x = x.replace(/\d+/, "");
console.log(x);

output:

asd

CODEPEN DEMO

If you really need to split the string you can use:

var x = "123442234535asd";
x= x.split(/\d+/);
Sign up to request clarification or add additional context in comments.

3 Comments

Wait, is the + after the digit matching shortcut needed there? I was thinking global g was what was needed, but it turns out here that either works.
@VítorMartins, se a minha resposta te ajudou, por favor considera aceitá-la como a resposta correta, obrigado !
@Pedro Lobito , desculpa ainda não podia aceitar tinham que passar uns minutos, já aceitei, obrigado ;)
2
x.split(/[0-9]+/);

the last element in the array would be 'asd'

1 Comment

yeah my bad, forgot about that...:)
1

If you want to split and only keep non-empty elements in the array that you get, use .filter(Boolean) after splitting:

var x = "123442234535asd";
alert(x.split(/[0-9]/g).filter(Boolean));

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.