0

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

1
  • 2
    I don't think this has much to do with jQuery - that is a javascript framework for DOM manipulation. You're doing string parsing, everything you need is in the javascript language. Commented May 25, 2011 at 8:37

5 Answers 5

4

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.

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

Comments

2

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.

Comments

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

Comments

1

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;

Comments

0

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,"");

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.