sammy_jacob
sammy_jacob

Reputation: 205

how to extract string part and ignore number in jquery?

I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.

Upvotes: 0

Views: 2056

Answers (5)

Kevin
Kevin

Reputation: 11

Find some more information about regular expressions in javascript...

This should do what you want:

var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");

This replaces al numbers in the entire string. If you only want to remove at the end then use this:

var re = /[0-9]*$/g;

Upvotes: 1

silex
silex

Reputation: 4320

var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150080

I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.

var yourString = "foobar1, foobaz2, barbar23, nobar100";    
var yourStringMinusDigits = yourString.replace(/\d/g,"");

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272406

This can be done in JavaScript:

/^[^\d]+/.exec("foobar1")[0]

This will return all characters from the beginning of string until a number is found.

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1075765

If you want to strip out things that are digits, a regex can do that for you:

var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
// "foobar"

(\d is the regex class for "digit". We're replacing them with nothing.)

Note that as given, it will remove any digit anywhere in the string.

Upvotes: 4

Related Questions