Skip to content

Instantly share code, notes, and snippets.

@1Marc
Created April 25, 2014 04:24
Show Gist options
  • Save 1Marc/11277624 to your computer and use it in GitHub Desktop.
Save 1Marc/11277624 to your computer and use it in GitHub Desktop.
Regex keyword filter spec
Keywords here are "SVG" and "CSS3"
The regex should find only words found in the text or in individual keywords in a URL seperated by dashes. It should NOT match any keywords located in shortned URL hashes.
Should match:
"I love this thing about SVG. http://foo.com/article"
"This is awesome http://super-awesome-svg.com"
"I <3 this css3 button thing http://codepen.com/abcdefg"
Should NOT match:
"This is a thing we are testing. It should not match this http://t.co/ifesVgief"
"Boogy buggy booger. It should not match this http://t.co/ewfijCSs3few"
@tednaleid
Copy link

// match any string that contains either of the whole words svg or css (\b means word boundary, which works for "-"
var r = /.*\b(svg|css3)\b.*/i;
r.exec("I love this thing about SVG. http://foo.com/article");
["I love this thing about SVG. http://foo.com/article", "SVG"]
r.exec("This is awesome http://super-awesome-svg.com");
["This is awesome http://super-awesome-svg.com", "svg"]
r.exec("I <3 this css3 button thing http://codepen.com/abcdefg")
["I <3 this css3 button thing http://codepen.com/abcdefg", "css3"]
r.exec("This is a thing we are testing. It should not match this http://t.co/ifesVgief")
null
r.exec("Boogy buggy booger. It should not match this http://t.co/ewfijCSs3few")
null

@1Marc
Copy link
Author

1Marc commented Apr 25, 2014

Ted -- that looks great!

Looks like @jonfriskics got it working as well --> https://gist.github.com/anonymous/11278042

var reg = new RegExp(/(\bcss3|svg\b)+|https?://(www.)?\w+.\w+\/(-css3|svg-)/ig);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment