How do you match all of the words without colon in regex

I’m using match() to check if the words do not have “:” colon. How do I do that in regular expression?

Could you use a filter that checks for no colons, so that you end up with only non-colon words?

function noColons(word) {
    return !word.match(/:/);
}
var str = 'this that: :more that ot:her that:too other',
    result = str.split(' ').filter(noColons).join(' ');

// result = ['this that other']

Good question, there’s probably a good way with lookbehind / lookahead.

Testing singular words is easy enough as \w matches word characters which don’t include :

"Th:is".match(/^\w+$/)
"This".match(/^\w+$/)

Testing a sentence is more difficult, I’m not sure how to do it in a single regex so I’d do it in two steps.

var matches = "Th:is is another test".match(/[\w:]+/g);
var filtered = matches.filter(function(str) { return str.indexOf(':') == -1 });

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.