1

I need a regex expression that performs a "contains" function on a input/type=hidden field's value. This hidden field stores unique values in a comma delimited string.

Here's a scenario.
The value: "mix" needs to be added to the hidden field. It will only be added if it does not exist as a comma delimited value.

With my limited knowlege of regex, I can't prevent the search from returning all ocurrences of the "mix" value. For example if: hiddenField.val = 'mixer, mixon, mixx', the regex always returns true, because the all three words contain the "mix" characters.

Thanks in advance for the help

4 Answers 4

3

You can use \b metacharacter to set word boundaries:

var word = "mix";
new RegExp("\\b" + word + "\\b").test(hidden.value);

DEMO: http://jsfiddle.net/ztYff/


UPDATE. In order to protect our regular expression of possible problems when using variable instead of hardcoding (see comments below), we need to escape special characters in word variable. One possible solution is to use the following method:

RegExp.escape = function(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

DEMO: http://jsfiddle.net/ztYff/1/


UPDATE 2. Following @Porco's answer we can combine regular expressions and string splitting to get another universal solution:

hidden.value.split(/\s*,\s*/).indexOf(word) != -1;

DEMO: http://jsfiddle.net/ztYff/2/

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

7 Comments

This is not a good idea. You really should regex-quote the word to avoid run-time errors.
@Tomalak Not really got what you mean. Could you give an example.
What if word contains a backslash? Or a dollar sign? This will break your solution. (Not in this special case, but generally when you build regex from strings.)
@Tomalak Ah... well, it will break any universal solution like this. Of course, word should be secured. Let me think about the better way.
@Tomalak Now regexp should be protected.
|
2
hiddenField.val.split(',').indexOf('mix') >= 0

Could also use a regex(startofstring OR comma + mix + endofstring OR comma):

hiddenField.val.match(/(,|^)mix($|,)/)

4 Comments

Array.prototype.indexOf is only supported in IE9 though.
the MDN has a backwards-compatible implementation of indexOf() for browsers that are missing it: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
added regex for alternative method
I like the simplicity of your answer. I'll need to do some performance tests to see if the two operations: split and indexOf will perform faster than a single regex expression.
0
hiddenField.val.match(/^(mix)[^,]*/);

Comments

0

How about:

/(^|,)\s*mix\s*(,|$)/.test(field.value)

It matches mix, either if it is at the beginning of a string or at the end or in between commas. If each entry in the list can consist of multiple words, you might want to remove the \s*.

Comments

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.