0

I'm almost embarrassed to say that I can't figure out a better way to do this. Can someone help me with refactoring this code? I tried doing a few "or" operators in one match function, but it didn't seem to take em.

$('.ui-icon-pencil, .ui-icon-check, .ui-icon-close, .ui-icon-trash').hover(function(e) {          

    console.log($(this).attr('class'));

    optionsClass = 'ui-icon-pencil';
    confirmClass = 'ui-icon-check';
    closeClass = 'ui-icon-close';
    deleteClass = 'ui-icon-trash';

    icon = $(this).attr('class').match(optionsClass);

    if(icon == null) {
    icon = $(this).attr('class').match(confirmClass);
    }
    if(icon == null) {
    icon = $(this).attr('class').match(closeClass);
    }
    if(icon == null) {
    icon = $(this).attr('class').match(deleteClass);
    }

    console.log('icon = '+icon);

});

2 Answers 2

2

When you utilise a RegExp, do it in the right way:

$('.ui-icon-pencil, .ui-icon-check, .ui-icon-close, .ui-icon-trash').hover(function(e) {
    var icon = /\bui-icon-(?:pencil|check|close|trash)\b/.exec(this.className);
    if (icon) { // If a match is found
        icon = icon[0];
    }
    console.log('icon = ' + icon);
});
  • this.className holds a (white-space separated) list of class names
  • /ui-icon- ... / is a RegExp literal.
  • regex.exec(input_string) is equivalent, but neater than input_string.match(regex).

When a match is found, icon will be an array in the following format:

[full match, group1, group2, ..., groupN]

To see the full match without the separate groups, use icon[0] (first element of icon array).

The string.match converts the first argument to a RegExp. Generally, regex.exec is preferred over string.match, because the latter method would fail if the input is not a string (unexpected?).

Explanation of my RegEx: \b matches a word boundary. (?:pencil|check|close|trash) is a dereferenced RegEx group, which matches "pencil", "check", "close" or "trash".

Sign up to request clarification or add additional context in comments.

5 Comments

Can you explain what this does?
@Catfish I have added references + a brief explanation at the bottom of my answer. Are you familiar with Regular expressions?
This doesn't quite work. It returns things like icon = ui-icon-pencil,pencil. I need just ui-icon-pencil.
It's an array. icon[0] will give you the first element.
@Catfish Updated answer with more explanation, plus [0].
1
$('.ui-icon-pencil, .ui-icon-check, .ui-icon-close, .ui-icon-trash').hover(function(e) {          

    var icon = $(this).attr('class').match(/ui-icon-(?:pencil|check|close|trash)/)[0]
    console.log('icon = '+icon);

});

1 Comment

.match() returns an array of matches found, [0] returns the whole match found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.