javascriptnsregularexpression

what would be regex expression for js


what would be the regular expression for

JIRA #R6K4118 

I am using this, but it's not working:

patt=/JIRA #[a-zA-Z]{1,}\d+[a-zA-Z]{1,}[0-9]{1,}/gi; patt.exec(patt); comment=comment.replace(patt,"JIRA #$1$2$3$4");

It's returning JIRA#R-6

Where am I going wrong?

the desired output is

https://eteamproject.internal.ericsson.com/projects/R6K4118


Solution

  • Assuming the string after # is structured just like the example then the regex is:

    (Assuming you want to match the whole string)

    /JIRA #[a-z]\d[a-z]\d{4}/i

    The /i in the end makes it case insensitive, so the we don't need to match both upper and lowercase letters.

    const expression = /JIRA #[a-z]\d[a-z]\d{4}/i;
    const result = "JIRA #R6K4118".match(expression);
    console.log(result);

    If you want to match just the string after # then use a positive lookbehind:

    /(?<=JIRA #)[a-z]\d[a-z]\d{4}/i

    const expression = /(?<=JIRA #)[a-z]\d[a-z]\d{4}/i;
    const result = "JIRA #R6K4118".match(expression);
    console.log(result);