0

How can I get only 'ABCD' from string 'ABCD150117T15' in java script.

I would like strip rest of the string from 'ABCD' in this example and generally everything until but excluding the first number character.

thanks

4
  • Don't understand what exactly you wanted to do...you could achieve it by using .substring function on string. Commented Nov 2, 2015 at 20:33
  • I would like to get only word before numeric number starts. Commented Nov 2, 2015 at 20:34
  • 1
    Go ahead and add that to your question. "Match text before numbers start" Commented Nov 2, 2015 at 20:41
  • Edited and answered accordingly :) Commented Nov 2, 2015 at 21:13

3 Answers 3

1

You can match for a-z

'ABCD150117T15'.match(/^[a-z]+/i)

you can match anything that is not a number

'ABCD150117T15'.match(/^[^\d]+/)
Sign up to request clarification or add additional context in comments.

Comments

1

Use regex then to match the first digit, and then subtring it to the text;

var str = 'ABCD150117T15';
var index = str.search(/\d/);
var text = str.substr(0,index);

2 Comments

Wouldn't it just be index and not index+1? (I'm not familiar with str.search)
EDITED : My bad, I totally forgot string.seach return an 0_started index like substr
1
'ABCD150117T15'.match(/^\D+/)[0]

This would give you everything until the first number then :)

3 Comments

I doubt the string is always ABCD, but who knows
Actually I may get sometimes ABCD150117T15/A150117T15. It's not a hard coded value. I am looking for word/letter before number starts in the string
adjusted it accordingly

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.