0

I am still new on regex (I should learn), I just need JS regex to extract string and number from a text like this: [number][string].

( /^(\d+).(\s+)/?)

Any help please?

1
  • Can you show some text you need to extract from? Commented Apr 7, 2012 at 10:40

2 Answers 2

2

I think you are looking for '/\d+[a-z]+/gi' :

var x = "123123K, 333D".match(/\d+[a-z]+/gi); 
console.log(x)​;    //["123123K", "333D"]

UPDATE: x is array. You can iterate over it and extract number and text:

xitem.match(/\d+/)[0]      //number
xitem.match(/[a-z]+/i)[0]  //text

P.S.: About \s:

Matches a whitespace character, which in ASCII are tab, line feed, form feed, carriage return, and space; in Unicode, also matches no-break spaces, next line, and the variable-width spaces (amongst others).

http://en.wikipedia.org/wiki/Regular_expression#Examples

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

4 Comments

sorry Engineer, I was not clear : for example if I have 123123D I want to extract 123123, and D
That updated part with two regexes will also match strings like "D123" and "1D1D1"... Makes more sense to capture the two interesting parts: console.log("123123K".match(/(\d+)([a-z]+)/gi));.
your solution dcoder seems to be the union of the two regexp of enginer. thanks to all.
@DCoder After /\d+[a-z]+/gi regexp matching "D123" and "1D1D1" can't be in matched pieces.All pieces are of "{number}{text}" kind.
0

If the square brackets are in the line as well then do the following:

/^\[(\d+)\]\[(\s+)\]$/

If they are not

/^(\d+)(\s+)$/

2 Comments

I am trying this but x results null:var x = "123123M".match(/^(\d+)(\s+)$/); console.log(x);
\s is used to match whitespace. "123123M" doesn't have whitespace.

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.