0

I can't find the magic formula to get my regex working in Javascript. let's say I have:

const branchnames = [
    { name:'WICO-29', key:1 },
    { name:'WICO-9', key:2 },
    { name:'wico-2', key:3 },
    { name:'wiCo-2: description', key:4 },
    { name:'WiCO-2 example ', key:5 },
    { name:'WiCO-2-dosomething', key:6 },
    { name:'wiCO-2, great', key:7 },
];

I would like to recover every names excepts for the first two. Hence I am looking for a regex looking for "wico-2" (not case sensitive), followed either by nothing or a non alphanumerical character. Following attempts didn't work...

const reg = new RegExp(/wico-2[ ,:-]?$/, "i"); // miss: 4,5,6,7
const reg = new RegExp(/wico-2\W/, "i"); // miss: 3
const reg = new RegExp(/wico-2\W?/, "i"); // wrong:1
const reg = new RegExp(/wico-2\W?$/, "i"); // only 3

branchnames.forEach( element => {
    console.log ( element.name.match(reg));
});

any help ? thanks !

7
  • please add the wanted result of the regex. Commented Oct 13, 2016 at 13:58
  • 2
    wait a sec.... Wico or Jico ? :D Commented Oct 13, 2016 at 14:00
  • Why you don't use if (key > 2)? Commented Oct 13, 2016 at 14:01
  • 1
    @Mohammad why would you? key != name Commented Oct 13, 2016 at 14:01
  • @RokoC.Buljan Because I would like to recover every names excepts for the first two. Commented Oct 13, 2016 at 14:04

4 Answers 4

1

I would use /wico-2(?:\D+|$)/ to do this:

const branchnames = [
    { name:'WICO-29', key:1 },
    { name:'WICO-9', key:2 },
    { name:'wico-2', key:3 },
    { name:'wiCo-2: description', key:4 },
    { name:'WiCO-2 example ', key:5 },
    { name:'WiCO-2-dosomething', key:6 },
    { name:'wiCO-2, great', key:7 },
];

const reg = new RegExp(/wico-2(?:\D+|$)/, "i"); 

branchnames.forEach( element => {
    console.log ( element.name.match(reg));
});

Explanation of (?:\D+|$):

  • Non-capturing group (?:\D+|$)
    • 1st Alternative \D+ : matches any character that's not a digit (equal to [^0-9])
    • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    • 2nd Alternative $: asserts position at the end of the string
Sign up to request clarification or add additional context in comments.

5 Comments

@bertrand By the way, the ?: only makes the parentheses not count as matching groups. You could leave that out to make the regular expression easier to read, possibly at the expense of speed.
Thank you. Actually, in this case speed is clearly not an issue, and I am in favor of something easy to read / understand ... actually before the answers from the community, I was doing :
const regexp1 = currentTaskKey + '\\W'; const reg1 = new RegExp(regexp1, 'i'); const regexp2 = currentTaskKey + '$'; // task key not followed by anything const reg2 = new RegExp(regexp2, 'i'); return element.name.match(reg1) || element.name.match(reg2);
const regexp1 = currentTaskKey + '\\W'; const reg1 = new RegExp(regexp1, 'i'); const regexp2 = currentTaskKey + '$'; const reg2 = new RegExp(regexp2, 'i'); return element.name.match(reg1) || element.name.match(reg2);
(sorry for the mess... comments cannot be formatted...)
1

You can use regex as /wico-2(?!\d)/gi

const branchnames = [
    { name:'WICO-29', key:1 },
    { name:'WICO-9', key:2 },
    { name:'wico-2', key:3 },
    { name:'wiCo-2: description', key:4 },
    { name:'WiCO-2 example ', key:5 },
    { name:'WiCO-2-dosomething', key:6 },
    { name:'wiCO-2, great', key:7 },
];
const reg = new RegExp(/wico-2(?!\d)/, "gi");

branchnames.forEach( element => {
  if(element.name.match(reg)){
     console.log (element);
  }
});

1 Comment

Your example will select also fico-3: wico-26
0

One might do as follows;

var branchnames = [
    { name:'WICO-29', key:1 },
    { name:'WICO-9', key:2 },
    { name:'wico-2hello', key:3 },
    { name:'wico-2', key:3 },
    { name:'wiCo-2: description', key:4 },
    { name:'WiCO-2 example ', key:5 },
    { name:'WiCO-2-dosomething', key:6 },
    { name:'wiCO-2, great', key:7 },
];
branchnames.forEach(bn => /wico-2(?=[^\dA-Za-z]|$)/i.test(bn.name) && console.log(bn.name));

1 Comment

like WiCO-2-dosomething? @RokoC.Buljan
0

You could do this:

/wico-2(([ ,:-]+|$).*$)/

Here is a link to regex tester:Regex

enter image description here

1 Comment

"Don't" use spaces in regex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.