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.
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"
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
Code
I don't wanna spoon-feed you, but the next step is to Check whether a string matches a regex in JS.
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.
Related
I have the following string:
"By signing in, I agree to the {{#a}}[Terms of Use](https://www.example.com/termsofuse){{/a}} and {{#a}}[Privacy Policy](https://www.example.com/privacy){{/a}}."
And I am using the following regex to split the words while considering {{#a}}[Terms of Use](https://www.example.com/termsofuse){{/a}} and {{#a}}[Privacy Policy](https://www.example.com/privacy){{/a}} as whole words.
\s+(?![^\[]*\])
My problem is that my current regex does not remove the full stop at the end of {{#a}}[Privacy Policy](https://www.example.com/privacy){{/a}}.. Ideally I would like my regex to split full stops, exclamation marks and question marks. That being said, I'm not sure how would I differentiate between a full stop at the end of the word and a full stop that is part of the URL.
You can try a variation of the following regular expression:
\s+(?![^\[]*\])|(?=[\.?!](?![a-zA-Z0-9_%-]))
The new part being the alternation of (?=[\.?!](?![a-zA-Z0-9_%-])) at the end. It performs a positive lookahead of a period, question mark or bang, using a negative lookahead to make sure it's not followed by a URL-ish looking character. You may need to adjust that character class in brackets to contain the characters you want to consider part of the URL.
Instead of .split you will be better off using .match here using this regex:
\{\{#a}}.*?\{\{\/a}}/g
This matches {{#a}} followed by 0 or of any character followed by {{/a}}.
or else you may use this more strict regex match:
\{\{#a}}\[[^\]]*]\([^)]*\)\{\{\/a}}
Here:
\[[^\]]*]: Matches [...] substring
\([^)]*\): Matches (...) substring
RegEx Demo
var string = "By signing in, I agree to the {{#a}}[Terms of Use](https://www.example.com/termsofuse){{/a}} and {{#a}}[Privacy Policy](https://www.example.com/privacy){{/a}}.";
console.log( string.match(/\{\{#a}}.*?\{\{\/a}}/g) );
Good day. I wanna detect the url string in the <a> tag
Link
whether it matchs the pattern : ?post_type=tribe_events&p=#### (#### = 4 digits number)
I'm writing some Jquery code to detect the expression but the console is throwing the error :
Invalid regular expression: /^(?)post_type=tribe_events&p=^(d{4})/:
Invalid group
var str = $(a).attr("href");
var regexEx = /^(?)post_type=tribe_events&p=^(d{4})/;
var ok = regexEx.exec(str);
console.log(ok);
I'm not good at the regex so I'd be aprreciated if there's any help.
There are couple of issues in your regex.
You need to remove ^ from your regex which denotes start of string and in your case your string doesn't actually start from a ? and is in middle of the string.
You need to escape ? as it has special meaning in regex which is zero or one occurrence of a character.
You need to remove second ^ after p= which isn't needed
You need to write \d and not just d for representing a number.
Also you don't need to group ? and \d{4} unless you really need them.
You corrected regex becomes,
\?post_type=tribe_events&p=\d{4}
Demo
If the test is really what you want, I suppose the right syntax would be:
/^\?post_type=tribe_events&p=\d{4}/
Can you please help me with the regular expression. I am newbie to this.
my requirement is I want to extract the vehicle no (i.e, 123456789) from the below url :
mysite.com/resource?slk=121&ops=rewww&from=kld&to=aop&search=things&validVehicle=sdfdsdff-sdfdf-sddf%3AVX%3ALNCX%3A123456789%3AOPW%3ALOS
I tried the below expression:
[&?]{1}validVehicle[=]{1}[^&]*[%3A]{1}([^%&]+)
But it is giving invalid results. Can you pelase help me on this.
A pure regex solution:
[&?]validVehicle=[^&]*(\d{9})
Or, if you are sure they appear after %3A and not followed with a digit:
[&?]validVehicle=[^&]*%3A(\d{9})(?!\d)
See this regex demo and another regex demo. The value you seek is in Group 1.
Details:
[&?] - a ? or &
validVehicle= - a literal substring
[^&]* - any symbols other than &, as many as possible up to the last
%3A - literal substring
(\d{9}) - Group 1: 9 digits
(?!\d) - not followed with a digit.
A "structural" approach might be to use those "%3a" colons as the delimiters of the pattern, combined with non-greedy wildcards .* (this matches fourth field of 'validVehicle' as defined by the delimiter %3a, and assumes this structure does not change):
[&?]validVehicle=(?:.*?%3a){3}(.*?)%3a
The utility of this way vs the \d{9} patterns already suggested really just depends on what you know for certain about the incoming data. Such patterns would certainly match nine digits in other fields of that delimited value.
Can someone please help me in defining a regular expression for an endpoint.
person/^((?!-).)*$/
This regex needs to match a number of things but mainly:
person/:id
it should NOT match
person/1234-5678-9123 (it's currently not matching this which is good)
the problem I have is that it should NOT match this but it is:
person/123456789123 (it's currently matching this but shouldn't)
To be clear, If you go to: http://regex101.com and paste in:
^((?!-).)*$
You can see that is matches 123456789123 WHICH IS WRONG
How can I change the RegEx so it doesn't match 123456789123
Cheers.
Your regex ^((?!-).)*$ is same as ^[^-]*$ that is match any charcater but not of - zero or more times.
The reason for why your regex not matches this person/1234-5678-9123 is because it has - symbol. But person/123456789123 string isn't has - symbol, so this got matched.
To match the string which has - between the numbers then you could try the below regex.
^.*?\d+-\d+.*$
OR
^(?=.*?-).+$
(?=.*?-) Positive lookahead asserts that the string must contain an - symbol.
DEMO
Can someone please explain the syntax of searching through strings? For example, I have this piece of code:
var ok = phone.value.search(/^\d{3}-\d{4}$/);
phone is a variable that is supposed to contain a phone number, and I know from context that this is supposed to make sure the inputted number has the format ###-####, but I don't know what the code within the parenthesis means or how it is evaluated. If someone has a link explaining how to use code like that I would especially appreciate it.
That's a regular expression ( regex ),
Regex One has a good guide on how to use them
Your regex says "beginning with 3 digits, then a "-" then 4 digits"
It's a regular expression, a whole world in itself.
http://www.regular-expressions.info/tutorial.html
It is regex object. The ^ matches the beggining of the string, the \d{3} matches 3 digits, the - matches a dash, the \d{4} matches for digits, and finally the $ matches the end of the string.
What you have there is called a "regular expression" and as you say, they are used to ensure input matches a certain pattern. I recommend you go somewhere like http://www.regular-expressions.info/ for further info rather than re-post data here.