Ad

How To Select String Between Two Specific Characters?

- 1 answer

I have some classname which I get them like this:

var classname = $(this).attr("class");

Here is a two examples of those them:

fa fa-bold
fa fa-bell-o separator

Now I need to these results:

bold
bell-o

The clue is: everything after fa- and before . How can I do that?

Ad

Answer

The clue is: everything after fa- and before . How can I do that?

You could capture all non-space characters after fa-:

/fa-(\S+)/

Then use the .match() method:

var match = 'fa fa-bell-o separator'.match(/fa-(\S+)/);
var result = match ? match[1] : null;
// "bell-o"
Ad
source: stackoverflow.com
Ad