-4

I have a sample format for which I want the regular expression in javascript. The format is as below.

I-KA-BGLK-ENB-V001

I am unable to try as I dont know much about the Regex Please let me know how to get it.

Even If I get the regex it will do, the javascript part I can handle it.

Nad
  • 4,605
  • 11
  • 71
  • 160
  • what do you want to do with the regex? validate string to be of this format? – Inus Saha Jun 22 '18 at 11:35
  • 1
    Your question is very vague. How do we infer the format from that sample? You should give examples and tell the format explicitly. Read about regular expressions here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp – kiddorails Jun 22 '18 at 11:36
  • @InusSaha: yes, I want to validate it. so user cant enter the invalid string – Nad Jun 22 '18 at 11:36
  • 2
    *Unable to try as I dont know much about the Regex* is incredibly lazy. Go through a basic tutorial, then come back with a more specific question. https://codeburst.io/javascript-learn-regular-expressions-for-beginners-bb6107015d91 – ohaal Jun 22 '18 at 11:36
  • "Unable" to even *try*?! :/ I mean, ok, not knowing something is fine, not even trying... I don't get it. Maybe it's a good time to learn about regex-they're pretty important. And handy. – Dave Newton Jun 22 '18 at 11:39

3 Answers3

1

try this

var str = 'I-KA-BGLK-ENB-V001';

var re = /^[A-Z]-[A-Z]{2}-[A-Z]{4}-[A-Z]{3}-[A-Z]\d{3}$/;

re.test(str);// true

[A-Z] - means any uppercase letter

\d - means any digit 0-9

\d{3} - means 3 digits

[A-Z]{2} - means 2 uppercace letters

You can change if you need digits in some places.

If you dont care about lowercase or uppercase replace [A-Z] with \w

https://github.com/zeeshanu/learn-regex - lessons Or you google "learn regex easy"

0

The valid regular expression for this is:

^\w-\w{2}-\w{4}-\w{3}-\w{4}$
^\w-\w{2}-\w{4}-\w{3}-\w\d{3}$

Explanation

exp1

expl

Code

I don't wanna spoon-feed you, but the next step is to Check whether a string matches a regex in JS.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Well to match strings in the I-KA-BGLK-ENB-V001 format you can use this regex:

^[A-Z]\-[A-Z]{2}\-[A-Z]{4}\-[A-Z]{3}\-\w{4}$

You can test it in Regex101, where you can see an example of matching strings and check the meaning and the specifications for each part of it.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78