Regular expression to get class name with specific substring

I need a regular expression in javascript that will get a string with a specific substring from a list of space delimited strings.

For example, I have;

  • widget util cookie i18n-username

I want to be able to return only i18n-username.




You could use the following function, using a regex to match for your string surrounded by either a space or the beginning or end of a line. But you'll have to be careful about preparing any regular expression special characters if you plan to use them, since the search argument will be interpreted as a string instead of a RegExp literal:

var hasClass = function(s, klass) {
  var r = new RegExp("(?:^| )(" + klass + ")(?: |$)")
    , m = (""+s).match(r);
  return (m) ? m[1] : null;

hasClass("a b c", "a"); // => "a"
hasClass("a b c", "b"); // => "b"
hasClass("a b c", "x"); // => null

var klasses = "widget util cookie i18n-username";
hasClass(klasses, "username"); // => null
hasClass(klasses, "i18n-username"); // => "i18n-username"
hasClass(klasses, "i18n-\\w+"); // => "i18n-username"

As others have pointed out, you could also simply use a "split" and "indexOf":

var hasClass = function(s, klass) {
  return (""+s).split(" ").indexOf(klass) >= 0;

However, note that the "indexOf" function was introduced to JavaScript somewhat recently, so for older browsers you might have to implement it yourself.

var hasClass = function(s, klass) {
  var a=(""+s).split(" "), len=a.length, i;
  for (i=0; i<len; i++) {
    if (a[i] == klass) return true;
  return false;


Note that the split/indexOf solution is likely faster for most browsers (though not all). This jsPerf benchmark shows which solution is faster for various browsers - notably, Chrome must have a really good regular expression engine!

function getString(subString, string){
    return (string.match(new RegExp("\S*" + subString + "\S*")) || [null])[0];

To Use:

var str = "widget util cookie i18n-username";
getString("user", str);  //returns i18n-username

Does this need to be a regex? Would knowing if the string existed be sufficient? Regular expressions are inefficient (slower) and should be avoided if possible:

var settings = 'widget util cookie i18n-username',
    // using an array in case searching the string is insufficient
    features = settings.split(' ');

if (features.indexOf('i18n-username') !== -1) {
     // do something based on having this feature

If whitespace wouldn't cause an issue in searching for a value, you could just search the string directly:

var settings = 'widget util cookie i18n-username';

if (settings.indexOf('i18n-username') !== -1) {
    // do something based on having this value

It then becomes easy to make this into a reusable function:

(function() {
    var App = {},
        features = 'widget util cookie i18n-username';

    App.hasFeature = function(feature) {
        return features.indexOf(feature) !== -1;
        // or if you prefer the array:
        return features.split(' ').indexOf(feature) !== -1;

    window.App = App;

// Here's how you can use it:
App.hasFeature('i18n-username'); // returns true


You now say you need to return all strings that start with another string, and it is possible to do this with a regular expression as well, although I am unsure about how efficient it is:

(function() {
    var App = {},
        features = 'widget util cookie i18n-username'.split(' ');

    // This contains an array of all features starting with 'i18n'
    App.i18nFeatures = {
        return value.indexOf('i18n') === 0;

    window.App = App;

/i18n-\w+/ ought to work. If your string has any cases like other substrings can start with i18n- or your user names have chars that don't fit the class [a-zA-Z0-9_], you'll need to specify that.

var str = "widget util cookie i18n-username";

Edit: If you need to match more than one string, you can add on the global flag (/g) and loop through the matches.

var str = "widget i18n-util cookie i18n-username";
var matches = str.match(/i18n-\w+/g);

if (matches) {
    for (var i = 0; i < matches.length; i++)
    alert("phooey, no matches");


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.