0

I need to detect if a value in an input contains the letters 'sn' followed by a hyphen (not immediately, the input will be sn12345 - more text) and if it does, then run a function which works fine unless the user enters words such as "isn't" or "wasn't".

Is there a way that I can detect if there are any letters before "sn" and whether or not to run the function. I did consider checking for the apostrophe but some users are quite lazy when entering into the input in question and don't use them.

var lowercase_name = subject.toLowerCase();
var has_sn = lowercase_name.indexOf("sn") > -1;
    if(has_sn === true){
        var has_hyphen = lowercase_name.indexOf("-") > -1;
            if(has_hyphen === false){
                //alert user missing hyphen
            };
                return false;
            }
    }

I did also consider checking if the "sn" is preceded by a space but if it is used at the beginning of the input (which it most likely will in this instance) that will fail.

tatty27
  • 1,553
  • 4
  • 34
  • 73
  • 1
    Checking for `sn` and `-` separately in that way clearly makes no sense – because as you are doing it right now, it just needs a `-` _anywhere_ in the string. If you want to look for `sn-`, then why are you not using that for indexOf in the first place? / A regular expression might be better suited for this; it’ll allow you to check for a _word boundary_ before the `sn`, and also easily incorporate the sn-at-the-beginning part using the start-of-line anchor, `^`. – CBroe Apr 27 '16 at 21:38
  • I only need to search for a hyphen if sn exists but sometimes my users as spaces after the sn. The script as it is works fine except when people use words that have sn in them. – tatty27 Apr 27 '16 at 21:41
  • If you end up using regular expressions, you might find this useful: http://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent – Timothy Kanski Apr 27 '16 at 21:42
  • why are you concerned about isn't and wasn't if your 'sn' needs to be followed by a hyphen? Or does your string need to contain 'sn' and then some other characters, then a hyphen? – Olivier De Meulder Apr 27 '16 at 21:42
  • 2
    Watch for snails, snakes, and snapping turtles! – Timothy Kanski Apr 27 '16 at 21:43
  • @OlivierDeMeulder because the required format is sn12345 - error. the script uses the numbers or letters that follows the sn to identify a serial number of an item and the string after the hypen is used elsewhere – tatty27 Apr 27 '16 at 21:44
  • 1
    All this commentary wouldn't happen if you only provided a clear code-formatted sample and a list of case-sample. Timothy's comment is totally on point, you did not explained those cases and your real-world use case. – Roko C. Buljan Apr 27 '16 at 22:03
  • @RokoC.Buljan I'm doing the best I can, all learners have to start somewhere – tatty27 Apr 27 '16 at 22:15
  • ;) No problem! Just make sure to be concise and specific. Learning JS has nothing to do with asking common-sense questions. Next time if you'll ever have a regex issue, try to provide a specific list of *should-match* / *shoud-not-match*, than instead of 45 minutes (yours and ours) it would take 2 or 3 minutes to get an (as-well) good answer. :) Take care and happy coding! – Roko C. Buljan Apr 27 '16 at 22:23

1 Answers1

1

Since you say there may or may not be spaces between sn and your dash, and you want to NOT match cases where sn is preceded by other letters, you could use a regex like:

var lowercase_name = subject.toLowerCase();
var has_sn = lowercase_name.indexOf("sn") > -1;
    if(has_sn === true){
        var snIsNotPrecededByLetters = lowercase_name.match(/\bsn/g);
        var has_hyphen = lowercase_name.indexOf("-") > -1;
            if(has_hyphen === false){
                //alert user missing hyphen
            };
                return false;
            }
    }

The "\b" in the Regex String makes it only match the "sn" if no letters precede it.

Timothy Kanski
  • 1,861
  • 14
  • 20