\d less efficient than [0-9]

\d checks all Unicode digits, while [0-9] is limited to these 10 characters. For example, Persian digits, ۱۲۳۴۵۶۷۸۹, are an example of Unicode digits which are matched with \d, but not [0-9]. You can generate a list of all such characters using the following code: var sb = new StringBuilder(); for(UInt16 i = 0; i … Read more

How do you access the matched groups in a JavaScript regular expression?

You can access capturing groups like this: var myString = “something format_abc”; var myRegexp = /(?:^|\s)format_(.*?)(?:\s|$)/g; var myRegexp = new RegExp(“(?:^|\s)format_(.*?)(?:\s|$)”, “g”); var match = myRegexp.exec(myString); console.log(match[1]); // abc And if there are multiple matches you can iterate over them: var myString = “something format_abc”; var myRegexp = new RegExp(“(?:^|\s)format_(.*?)(?:\s|$)”, “g”); match = myRegexp.exec(myString); while … Read more

What is a non-capturing group in regular expressions?

Let me try to explain this with an example. Consider the following text: http://stackoverflow.com/ https://stackoverflow.com/questions/tagged/regex Now, if I apply the regex below over it… (https?|ftp)://([^/\r\n]+)(/[^\r\n]*)? … I would get the following result: Match “http://stackoverflow.com/” Group 1: “http” Group 2: “stackoverflow.com” Group 3: “https://stackoverflow.com/” Match “https://stackoverflow.com/questions/tagged/regex” Group 1: “https” Group 2: “stackoverflow.com” Group 3: “https://stackoverflow.com/questions/tagged/regex” But … Read more