Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet.

It's fine to use String.match() for finding a substring but realize that String.indexOf() is preferable if you have no need for regex: http://stackoverflow.com/a/4757501/1100355 https://stackoverflow.com/a/4757501/1100355

So you would end up with something like this:

var c = $(this).attr("class");
if (!c || c.indexOf('brush: js') < 0) {
 return true;
}
...

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet.

It's fine to use String.match() for finding a substring but realize that String.indexOf() is preferable if you have no need for regex: http://stackoverflow.com/a/4757501/1100355

So you would end up with something like this:

var c = $(this).attr("class");
if (!c || c.indexOf('brush: js') < 0) {
 return true;
}
...

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet.

It's fine to use String.match() for finding a substring but realize that String.indexOf() is preferable if you have no need for regex: https://stackoverflow.com/a/4757501/1100355

So you would end up with something like this:

var c = $(this).attr("class");
if (!c || c.indexOf('brush: js') < 0) {
 return true;
}
...
Bounty Awarded with 50 reputation awarded by Community Bot
added code snippet
Source Link
seand
  • 2.5k
  • 1
  • 20
  • 29

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet.

It's fine to use String.match() for finding a substring but realize that String.indexOf() is preferable if you have no need for regex: http://stackoverflow.com/a/4757501/1100355

So you would end up with something like this:

var c = $(this).attr("class");
if (!c || c.indexOf('brush: js') < 0) {
 return true;
}
...

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet.

It's fine to use String.match() for finding a substring but realize that String.indexOf() is preferable if you have no need for regex: http://stackoverflow.com/a/4757501/1100355

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet.

It's fine to use String.match() for finding a substring but realize that String.indexOf() is preferable if you have no need for regex: http://stackoverflow.com/a/4757501/1100355

So you would end up with something like this:

var c = $(this).attr("class");
if (!c || c.indexOf('brush: js') < 0) {
 return true;
}
...
added link to SO answer
Source Link
seand
  • 2.5k
  • 1
  • 20
  • 29

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet. (I'm assuming you're looking for the JS code snippets on your page so you can add a Run button for them.)

As for the actual method you're using for finding a substring, that's the correct way. If you were tryingIt's fine to match the entireuse classString.match() value as opposed to justfor finding a substring, you would use the but realize that ===String.indexOf() operator instead.is preferable if you have no need for regex: http://stackoverflow.com/a/4757501/1100355

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet. (I'm assuming you're looking for the JS code snippets on your page so you can add a Run button for them.)

As for the actual method you're using for finding a substring, that's the correct way. If you were trying to match the entire class value as opposed to just a substring, you would use the === operator instead.

It looks like whatever library you're using is generating properties and sticking them inside the class attribute, which I've never seen before and seems questionable (why can't they be attributes on the element?). Because they look generated, could you maybe end up with something like this:

<pre class="class-name: 'jsbox'; brush: js">

or this:

<pre class="brush: js; [another property]; class-name: 'jsbox'">

If so, then you may want to only look for one property (like brush: js) to determine if you're looking at a JS code snippet.

It's fine to use String.match() for finding a substring but realize that String.indexOf() is preferable if you have no need for regex: http://stackoverflow.com/a/4757501/1100355

Source Link
seand
  • 2.5k
  • 1
  • 20
  • 29
Loading
default

AltStyle によって変換されたページ (->オリジナル) /