Web Development

Regular Expressions

Regular Expressions is section 13 of Beginner’s Guide to JavaScript.

There are times you might need to search a pattern on a text. Does this word include in this sentence kind of a thing. We cannot use the equal comparison for that kind of a matching. We need something special pattern matching technique. This called Regular Expression.

A regular expression is a sequence of characters that forms a search pattern. The search pattern can be used for text search. Also it can be used to text replace operations.

When you search for data in a text, you can use this search pattern to describe what you are searching for. A regular expression can be a single character, or a more complicated pattern.

Look at the following example.

var patt = /design/i

/design/i  is a regular expression. The design is a pattern that to be used in a search. The i  is a modifier that modifies the search to be case-insensitive.

So the Regular Expressions always have the following syntax.

/pattern/modifiers;

Using String Methods

In JavaScript, regular expressions are often used with the two string methods: search() and replace(). The search() method uses an expression to search for a match, and returns the position of the match. The replace() method returns a modified string where the pattern is replaced.

search()

var str = “Let’s Design n Develop”;
var n = str.search(/design/i);

The variable n will assigned value 6 which is the position of design in the string text.

The search method will also accept a string, without slashes and modifiers, as search argument. The string argument will be converted to a regular expression.

replace()

var str = “Let’s Design n Develop”;
var n = str.replace(‘Design’, ‘Try’);

The replace() method will also accept a string as search argument.

Regular Expression

Regular expressions can make your search much more powerful.

Modifiers

Used to perform case-insensitive more global searches.

Modifier Description
i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping after the first match)
m Perform multiline matching

Patterns

Brackets are used to find a range of characters.

Expression Description
[abc] Find any of the characters between the brackets
[0-9] Find any of the digits between the brackets
(x|y) Find any of the alternatives separated with |

Metacharacters are characters with a special meaning.

Metacharacter Description
d Find a digit
s Find a whitespace character
b Find a match at the beginning or at the end of a word
uxxxx Find the Unicode character specified by the hexadecimal number xxxx

Quantifiers define quantities.

Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n

RegExp Object

In JavaScript, the RegExp object is a regular expression object with predefined properties and methods.

test()

The test() method is a RegExp expression method. It searches a string for a pattern, and returns true or false, depending on the result.

The following example searches a string for the character “e”.

var patt = /e/;
patt.test(“The best things in life are free!”);

Since there is an “e” in the string, the output of the code above will be true. You don’t have to put the regular expression in a variable first. The two lines above can be shortened to one.

/e/.test(“The best things in life are free!”);

exec()

The exec() method is a RegExp expression method. It searches a string for a specified pattern, and returns the found text. If no match is found, it returns null. 

The following example searches a string for the character “e”:

/e/.exec(“The best things in life are free!”);

Since there is an “e” in the string, the output of the code above will be e.


What’s Next?

Learn what includes in Strict Mode.

 

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW

About the author

Sandeep Likhar

Sandeep Likhar from Indore India, Blogger, eBook Designer and Founder of LetsDnD. Having five years of experience in industry as a Digital Publishing Expert and eBook Converter providing services to Authors, Publishers and distributors worldwide. Proficient in converting books into various formats HTML, epub, mobi, word, PDF including all major online platforms iTunes, Kobo, Kindle, Createspace, B&N, Smashwords and more.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
DON’T MISS OUT!
Subscribe To Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Give it a try, you can unsubscribe anytime.

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW