Regular expression to get class name using a specific substring

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

For example, I have:

  • widget util cookie i18n-username

I want to return only i18n-username.

how

+1
source share
4 answers

You can use the following function using a regular expression to match your string surrounded by a space or the beginning or end of a string. But you should be careful when preparing any special regular expression characters if you plan to use them, since the search argument will be interpreted as a string instead of the RegExp literal:

var hasClass = function(s, klass) { var r = new RegExp("(?:^| )(" + klass + ")(?: |$)") , m = (""+s).match(r); return (m) ? m[1] : null; }; hasClass("abc", "a"); // => "a" hasClass("abc", "b"); // => "b" hasClass("abc", "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 pointed out, you can also just use β€œsplit” and β€œindexOf”:

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

However, note that the "indexOf" function has been introduced in JavaScript a bit recently, so for older browsers you may 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; }; 

[change]

Note that the split / indexOf solution is likely to be faster for most browsers (though not for all). This jsPerf benchmark shows which solution runs faster for different browsers - especially Chrome should have a really good regex engine!

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

For use:

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

Do I need to be a regular expression? It would be enough to know if the string exists? 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 the space does not cause problems when searching for a value, you can simply search the string directly:

 var settings = 'widget util cookie i18n-username'; if (settings.indexOf('i18n-username') !== -1) { // do something based on having this value } 

Then it becomes easy to do this in 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 how you can use it: App.hasFeature('i18n-username'); // returns true 

EDIT

Now you say that you need to return all lines that start on another line, and this can be done with a regular expression, although I'm not sure how effective it is:

 (function() { var App = {}, features = 'widget util cookie i18n-username'.split(' '); // This contains an array of all features starting with 'i18n' App.i18nFeatures = features.map(function(value) { return value.indexOf('i18n') === 0; }); window.App = App; })(); 
0
source

/i18n-\w+/ should work. If your string has cases such as other substrings starting with i18n- , or your usernames have characters that do not match the class [a-zA-Z0-9_] , you need to specify this.

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


Edit: If you need to match more than one line, you can add a global flag ( /g ) and scroll 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(matches[i]); } else alert("phooey, no matches"); 
-1
source

Source: https://habr.com/ru/post/1480869/


All Articles