Kevin Bowersox
Kevin Bowersox

Reputation: 94469

Find Value in CSV with Regex

I am trying to determine if a CSV string contains a specific number (also String), in this case the number 3. I have wrote some script to attempt this but the result always returns null. The regex works when using an online testing tool, however not when utilized via script. Can anyone determine what I'm missing?

Here is my code:

var csv = ["1,25,3","3", "1", "1,9,10", "2,4,5,6,7,11,33,3", "2,1,2,12,15,27"];

function contains(param){
    var regex = /(,)?\D[3]\D(,)?/g;
    return param.match(regex);
}

for(var i = 0; i < csv.length; i++){
  console.log(contains(csv[i]));
}

Or if you prefer: JsFiddle

Upvotes: 1

Views: 1239

Answers (1)

Martin Ender
Martin Ender

Reputation: 44259

The problem is that your pattern requires a character (\D) before and after your 3. Since all 3s in your example are at the end of the string the second \D can never match. What you want is probably something like this:

var regex = /(?:^|\D)3(?!\d)/;

For the end of the string we use negative lookahead. That asserts that there is no digit. This is better than asserting that there is a non-digit character (because it works for the end of the string, too). Ideally, we would use the same for the beginning, but that is not supported by JavaScript. So we say, either we have the beginning of the string or a non-digit character. In fact (as Brad Koch pointed out), in this specific case, both conditions constitute a word boundary (a position between a character in [a-zA-Z0-9_] and one that is not or an end of the string). So you can simply use:

var regex = /\b3\b/;

However, if your input can include other characters than digits and commas (e.g. 1,text,2,a3b,somemoretext), none of these approaches are sufficient. Instead you need to check for commas explicitly:

var regex = /(?:^|,)3(?![^,])/;

Also, since you don't need the actual match, but only want to know whether there is a match, you can use test instead:

return regex.test(param);

This will give you a boolean instead of an array (which probably also makes is marginally more efficient).

Upvotes: 4

Related Questions