Using JavaScript regular expressions to match beginning and end of strings

This took me a little bit of time to figure out, so I thought I'd document it here so I don't have to do the research all over again.

I am working on an expression builder that allows you to create expressions for filtering a data set. One of the features allows you to create an expression that searches for values that contain, begin with, or end with a certain string. Here's a code snippet which shows functions that allow you pass in the search term as well as the data to search.

contains: function(searchFor, str){
var pattern = new RegExp(searchFor, 'gi');
return pattern.test(str);
},

beginsWith: function(searchFor, str){
var pattern = new RegExp("\^" + searchFor, 'gi');
return pattern.test(str);
},

endsWith: function(searchFor, str){
var pattern = new RegExp(searchFor + "\$", 'gi');
return pattern.test(str);
},

 

Add a Comment...

 

Comments

Absolutely NEW update of SEO/SMM package "XRumer 16.0 + XEvil 3.0": captchas regignizing of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another size-types of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? There are a lot of impessive videos about XEvil in You - 7/22/2017

Revolutional update of SEO/SMM software "XRumer 16.0 + XEvil 3.0": captcha breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another subtypes of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? There are a lot of demo videos about XEvil in YouTube. Good - 7/22/2017

what is the best mattress 2017 best mattresses for 2017 top ten mattresses 2017 best mattresses in 2017 best mattresses in 2017 best mattresses for 2017 best memory foam mattress 2017 best mattresses 2017 best mattresses for 2017 best mattress in 2017 best mattress 2017 the best rated mattress 2017 the best mattresses 2017 best rated mattress for 2017 consumer reports best mattress 2017 casper discount codes casper discount promo code casper print leesa coupons leesa promo codes lees - 7/22/2017

Absolutely NEW update of SEO/SMM software "XRumer 16.0 + XEvil 3.0": captchas solving of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another subtypes of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? You can find a lot of introducing videos about XEvil in Yo - 7/22/2017

Revolutional update of SEO/SMM package "XRumer 16.0 + XEvil": captcha solving of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another subtypes of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? There are a lot of impessive videos about XEvil in YouTube. See yo - 7/21/2017

Absolutely NEW update of SEO/SMM package "XRumer 16.0 + XEvil 3.0": captcha breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another categories of captcha, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? There are a lot of demo videos about XEvil in YouTube. See - 7/18/2017

Revolutional update of SEO/SMM software "XRumer 16.0 + XEvil": captchas solving of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another types of captcha, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? You can find a lot of impessive videos about XEvil in YouTube. Goo - 7/14/2017