How to split these strings in jquery?
- Mode1
- 2Level
I want to get only the numbers from the above two strings in jquery... The strings may be Mode11,Mode111,22Level,222Level
etc the characters Mode
and Level
wont change...
How to split these strings in jquery?
I want to get only the numbers from the above two strings in jquery... The strings may be Mode11,Mode111,22Level,222Level
etc the characters Mode
and Level
wont change...
You could do something like this:
var numbers = "Mode111".match(/\d/g).join("")
var alpha = "Mode111".match(/[a-z]/gi).join("")
I think there is an easier way with match collections but I can't find anything to show whether javascript supports them. I will see if I can find it.
You probably want the String.prototype.match
method:
var str = 'Mode1';
var match = str.match(/\d+/);
var number = match && +match[0];
// If `str` contained no numbers then number === null
The unary plus operator (+) casts its operand to an actual number (from a string containing numbers).
var str = 'Mode123';
var num = str.match(/\d/g).join('');
or
var num = str.replace(/\D/g,'');
var myString="Mode111"; var num =myString.replace(/[a-zA-Z]/g,"");