Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Javascript Regular Expressions

You're reading from   Javascript Regular Expressions Leverage the power of regular expressions to create an engaging user experience

Arrow left icon
Product type Paperback
Published in May 2015
Publisher
ISBN-13 9781783282258
Length 112 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Nonvisual constraints

Until now, all the constraints we have been putting on our patterns had to do with characters that could or couldn't be displayed, but Regex provides a number of positional constraints, which allow you to filter out some false positives.

Matching the beginning and end of an input

The first such set is the start and end of string matchers. Using the (^) caret character to match the start of a string and the ($) dollar sign to match the end, we can force a pattern to be positioned in these locations, for example, you can add the dollar sign at the end of a word to make sure that it is the last thing in the provided string. In the next example, I used the /^word|word$/g pattern to match an occurrence of word, which either starts or ends a string. The following image exemplifies the match of the regular expression when given a Text input:

Matching the beginning and end of an input

Using both the start and end character together assure that your pattern is the only thing in the string. For example if you have...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image