Comments on: Regular Expressions in JavaScript, part 2 https://j11y.io/javascript/regular-expressions-in-javascript-part-2/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: TJ Loh https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-791 Wed, 02 Sep 2009 16:16:53 +0000 https://j11y.io/?p=692#comment-791 Great Post! Thanks

]]>
By: James https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-790 Thu, 26 Mar 2009 08:26:40 +0000 https://j11y.io/?p=692#comment-790 Thanks for the comments!

@Valentino, that looks like a very useful tool, thank you!

@Luke, thank you for the suggestions! I had no idea exec and match behaved differently. I’ll add that to the post. Also, just subscribed to http://blog.stevenlevithan.com/ – looks great!

]]>
By: Luke https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-789 Thu, 26 Mar 2009 05:17:10 +0000 https://j11y.io/?p=692#comment-789 Great post. Regex is definitely something that more js developers need to learn. One correction and a couple suggestions for you, though…

Correction: str.match and regex.exec are only the same if the regex is not compiled with the g flag. Against a g regex, match does not return capture groups, but every instance of the full match. exec returns the first match and any capture groups and updates the regex’s lastIndex Property. Future calls to exec will start from that index (useful in a while loop).

var re = /f(w)1/g, str = "Calling all foo, calling all faa";
str.match(re); // ["foo","faa"]
str.match(re); // ["foo","faa"]
re.exec(str);  // ["foo","o"] and re.lastIndex === 15
re.exec(str);  // ["faa","a"] and re.lastIndex === 32

Suggestions:
Unless you specifically need the data in your groups, use a non-capturing group, vis /(?:f|ht)tps?/ won’t internally store or return the f or ht string. It makes for marginally faster code, but more importantly, you don’t have to specify unused variables for dead captures if you have important capture groups afterward.

And your camelCaseCSS function doesn’t need an inner regex. charAt(1) will do the work much faster.

function camelCaseCSS(property) {
    return property.replace(/-w/g, function(match){
        return match.charAt(1).toUpperCase();
    });
}

Another great resource for advanced regex know-how is Steven Levithan’s blog Flagrant Badassery http://blog.stevenlevithan.com/

]]>
By: Valentino https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-788 Tue, 24 Mar 2009 20:33:22 +0000 https://j11y.io/?p=692#comment-788 I suggest this online tool for testing and creating RegEx:

http://gskinner.com/RegExr/

When I started using it, creating regular expressions became actually fun!

]]>
By: Graham B https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-787 Tue, 24 Mar 2009 14:24:34 +0000 https://j11y.io/?p=692#comment-787 Huh, I never knew replace() could accept a function. Nice one 🙂

]]>
By: eugene https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-786 Tue, 24 Mar 2009 13:26:28 +0000 https://j11y.io/?p=692#comment-786 great post
after reading your regex on testing for http, https, http://ftp...

i dug up an old regex i had for this, and seems completely bloated compared to your. great work.

here is my old one (stripped from a class)

// site
Site:function()
{
	var pattern = /^(([f]|[h])(t?tp)(s?)(://))?((www)(.))?/;
	var site	= window.content.location.host;
	site		= site.replace(pattern, '');
	return site;
}
]]>
By: Baa https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-785 Mon, 23 Mar 2009 23:08:16 +0000 https://j11y.io/?p=692#comment-785 Great post James. Finally somebody encouraged me to really start learning regular expressions. Thanks! 😉

]]>
By: Ben Nadel https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-784 Mon, 23 Mar 2009 22:40:20 +0000 https://j11y.io/?p=692#comment-784 Awesome stuff. I am a huge fan of regular expressions in Javascript. I have not given the String.match() method a go yet. I will have to try that.

]]>
By: Diogok https://j11y.io/javascript/regular-expressions-in-javascript-part-2/#comment-783 Mon, 23 Mar 2009 22:25:13 +0000 https://j11y.io/?p=692#comment-783 awesome!

]]>