cc young
cc young

Reputation: 20223

quote "*/" when part of pattern matching

have code

obj.property.style.split( /\s*;\s*/ ).forEach( function(s) {...

the problem is that the */ in the pattern is serving as a comment delimiter.

for example

/* comment this out please
obj.property.style.split( /\s*;\s*/ ).forEach( function(s) {..
OOPS - NOT commented out */

what's the customary way to quote this in regexp?

Upvotes: 1

Views: 39

Answers (3)

user123444555621
user123444555621

Reputation: 153016

From the specs:

To specify an empty regular expression, use: /(?:)/

In your case: /\s*;\s*(?:)/

The ?: prevents the parens from capturing. If you don't care about captures, you might as well use /\s*;\s*()/

Upvotes: 0

user166390
user166390

Reputation:

The * qualifier is a shortcut for {0,}1 so:

/\s*;\s{0,}/

However, as this makes it slightly less-than-usual, I'd really consider just not commenting out the code with /* .. */ comments.


1 {n,} is specified in ECMAScript 5th Edition. However, MDC only lists {n,m}.

Upvotes: 2

Joseph
Joseph

Reputation: 119847

I assume that your code looks like this:

obj.property.style.split( /\s*;\s*/ ).forEach(function(s) {
  ...very...
  ...long...
  ...implementation...
});

Taking it from another perspective, rather than block commenting the entire block, why not extract the callback as a standalone function. That way, you only need one line for forEach() wherein you can safely line-comment it.

function eachHandler(s) {
  ...very...
  ...long...
  ...implementation...
}

//you just prevented the forEach!
//obj.property.style.split( /\s*;\s*/ ).forEach(eachHandler);

Upvotes: 0

Related Questions