5

This is a very simple question, but because I've only been doing this language for a week, the answer has not come to me. An error occurs in between the following two lines, because Regex is randomly null, but how would I check if it's null, so itd doesn't throw an error?

var Regex = /\<span class="currency-robux">([\d,]+)\<\/span\>/
var PriceSelling = data.match(Regex)[1]
Toothbrush
  • 2,080
  • 24
  • 33
  • I searched for null, but couldn't find it. I literally searched 2 minutes ago. Sorry for the duplicate. – user3341815 Feb 22 '14 at 21:48
  • 1
    [Parsing HTML with Regular Expressions](http://stackoverflow.com/q/1732348/1348195) for extra credit. – Benjamin Gruenbaum Feb 22 '14 at 21:48
  • 1
    @BenjaminGruenbaum: He's not parsing it, but just matching a part of it - for which regex is fine. – Bergi Feb 22 '14 at 21:51
  • It is in a for loop. After checking if it is null, assuming it is, it would just go to the next item in the loop, right? – user3341815 Feb 22 '14 at 21:56
  • 1
    @Bergi How is that not parsing HTML with regex? Writing a regular expression for something you _already_ have a data structure you can query for seems silly. The easiest thing to do in his case would be to do `document.querySelector(".currency-robux").textContent` anyway. ( One extra `createDocumentFragment` line and then query that if it's not in the DOM yet). No heart attacks if you have a space after the word `class` or multiple classes, or several attributes. – Benjamin Gruenbaum Feb 22 '14 at 21:57
  • @BenjaminGruenbaum: Depends on whether the string is from a DOM or not. And it's not "parsing" like in the question you linked, where it was tried to express the language syntax of HTML in regex - which is not done here, it expects a particular span. – Bergi Feb 23 '14 at 12:02
  • @Bergi Do you honestly believe a regular expression is the best tool for this task? It could go wrong in so many ways where throwing it into a fragment and querying it is dead simple and a lot less fragile... – Benjamin Gruenbaum Feb 23 '14 at 12:04
  • No, but depending on the circumstances it could be the simplest. – Bergi Feb 23 '14 at 12:06

2 Answers2

1

Like this:

var Regex = /<span class="currency-robux">([\d,]+)<\/span>/;
var PriceSelling = data.match(Regex);
PriceSelling = PriceSelling ? PriceSelling[1] : '';

if (PriceSelling.length < 1) {
    alert('Nothing!');
}
Toothbrush
  • 2,080
  • 24
  • 33
  • Thanks for fixing that part, but it is in a for loop, and it is not letting me use the continue statement to skip over that ID because it is "Illegal". How would I fix that? – user3341815 Feb 22 '14 at 22:30
  • @user3341815 Instead of `< 1`, just use `> 0`. – Toothbrush Feb 23 '14 at 10:09
1

You could either do it in two steps:

var result = data.match(Regex);
var PriceSelling = result != null ? result[1] : undefined;

or use the OR operator to use an empty array as the default result:

var PriceSelling = (data.match(Regex) || [])[1];
Bergi
  • 630,263
  • 148
  • 957
  • 1,375