Archive for the ‘Regular Expressions’ Category

Search a pattern using Regular Expression

November 10, 2008

Search a pattern in strings using regular expression method match. Lets see methods startswith and endswith pattern searching of the given string.

Example : startsWith – check whether if a string is starts with the specified prefix.

function startsWith()
{
var input = prompt(“Enter search value : “);
var regex = “^”+input;
var arr = new Array(“Ajax Books”, “Javascript”, “Java”, “JSP & Servlet”, “MySQL”, “Regular Expression”);
for(i=0; i<arr.length; i++){
str=arr[i];
if( str.match(regex) == input){
alert(“Matching string is : ” +str);
}
}
}

Regular Expression : ^ – matches the beginning of the input string
For instance, input is “Regular”
output : alert should be Matching string is Regular Expression

Example : endsWith – check whether if a string is ends with the specified suffix.

function endsWith()
{
var input = prompt(“Enter search value : “);
var regex = input+”$”;
var arr = new Array(“Ajax Books”, “Javascript”, “Java”, “JSP & Servlet”, “MySQL”, “Regular Expression”);
for(i=0; i<arr.length; i++){
str=arr[i];
if( str.match(regex) == input){
alert(“Matching string is : ” +str);
}
}
}

Regular Expression : $ – matches end of the input string
For instance, input is “Books”
output : alert should be Matching string is Ajax Books
Are you new to regular expressions, read here https://manikandanmv.wordpress.com/2008/11/04/using-regular-expressions-in-javascript/

Advertisements

Using Regular Expressions in Javascript

November 4, 2008

Regular expressions can be used to find the patterns in strings. There are lots of  special characters and methods used in regular expressions and each one having separate meaning. For more details about that read here http://docs.sun.com/source/816-6409-10/regexp.htm
In this blog am writing about how to use that regular expressions in javascript. Methods that use in regular expressions are exec, test, match, search, replace and split. Also am not going to explain about each methods because the above sun documents will guide you those things clearly.
Lets see an example that will help you to understand clearly.

Example1: Check whether the given string is Numeric or not.

Regular expression  /^[0-9]+$/ is used to find a string is numeric. In this

  • [0-9] – represents numeric digits only allowed.
  • + – matches any number of characters.
  • ^ – matches beginning of the line
  • $ – matches end of the line

test – test the match of a given input string with regular expressions. returns true if it matches correctly otherwise returns false.

function isNumeric(input)
{
var regexp = /^[0-9]+$/;
if(regexp .test(input))
{
return true;
}
return false;
}

Note : you can also use regular expression /d for matching numeric characters.

Example 2: Replace all occurrences of a string with substring

Suppose if you want to replace all hypen (-) characters with space in input string s = “12-November-2008”
Below code will replace it.

replace – method used to search and replace for a matching string with substring.

regular expression:  /-/g

/- matches the string hypen(-)
/g – matches all occurrences of the string.

var s = “12-November-2008”;
s = s.replace(/-/g,’ ‘);

output : “12 November 2008”

Special Parameters are

/g – enables global matching, in the above example it matches all hypen(-) characters.
/i – makes case insensitive.
/m – multi-line mode