Pages

Showing posts with label Regular Expressions. Show all posts
Showing posts with label Regular Expressions. Show all posts

Wednesday, July 27, 2011

Regular expression to remove all numbers from a string

This is in continuation to my previous post where I posted the code to remove characters that are not digits. This case is vice versa.

var pattern=/\d/g;
var myString="80AlphaDog55";
alert(myString.replace(pattern,""));

The result is AlphaDog

hope this helps :)

Regular expression to remove all alphabets in a string

Regular expressions are very handly when it comes to working with patterns. In my case working with patterns in javascript. It saves me pile of code that needs to be written in javascript to do pattern based validations.

Following is a simple line of code that helps me to replace all the non digit characters from a string

var pattern=/\D/g
var myStr="80Alpha5Dog5"
alert(myStr.replace(pattern,""))

The answer would be BOSS :)

Hope that helps :)