Where am I going wrong using regex? - javascript

var s = "07:05:45PM";
var meridian = s.match("/[AM PM]/i"); //matches AM or PM in s
console.log(meridian);
// Expecting output as [PM] but actual output is null
Trying to get whether it is AM or PM. I am not sure where I am going wrong.

Your problem is really made up of two problems: your regular expression syntax and the value you're passing to match.
Placing text inside of square brackets in a regular expression (e.g. [AM PM]) means "match any one character that is declared in here." So doing [AM PM] translates to "Match an A, a P, an M, or a space." You would usually see such an expression written as [AMP ] (or any order of those characters). To match what you're looking for, try something like this:
[AP]M
which means "match either an A or a P followed by an M".
Then there's the problem of what you're passing to match. match should take a regular expression literal, not a string. Basically, remove the quotation marks.
Your final code could look like this:
var s = "07:05:45PM";
var meridian = s.match(/[AP]M/i); //matches AM or PM in s
console.log(meridian);

You need a regular expression, not a string.
var s = "07:05:45PM";
var meridian = s.match(/[AP]M/i); //matches AM or PM in s
console.log(meridian);

You should be using groups not character sets.
[AM PM] matches a single character in that set (A or M or P or ).
(AM|PM) matches the entire string where | is an or. In the example, it will match AM or PM.
Second, the input to match should be a regular expression not a string (remove the double quotes).

Try one of these
/[AP]M/i or /(AM|PM)/i
Your current regex is matching anything with only one the letters [AMP ].
Also, don't wrap your regex in quotes, you want it to be a regex not a string.

Related

Javascript - Regular Expression with string template followed by 4 digits number?

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}/

JS - Regex for one letter and 6 numbers

I have this regular expression to test if an input starts with the letter "a" and is followed by 6 numbers. On the online validator seems to work, but on JavaScript doesnt.
This is the code:
function checkBookingReference (ref) {
var regex = /(^a|A)([0-9]{6})/;
return regex.test(ref);
}
The function returns true if I enter more than six numbers, and it shouldn't. Any idea why?
That regex will return true if anywhere in the string there is a match. If you want to ensure the entire string matches it, then you'll want to use ^ to match the beginning and $ to match the end.
/^(a|A)([0-9]{6})$/
This is how I would do it:
return /^A[0-9]{6}$/i.test(ref);
Use the regex object to specify the regular expression and then test it. Try this
var regex = new RegExp("^a([0-9]{6})$","i");
return regex.test(ref);
You nee to move the carat ^ outside the parentheses and use a proper group around the letters, then loose the trailing dollar sign $. Try this:
var regex = /^[aA][0-9]{6}/;
The parenthesis inside meant "not". Outside it means "beginning of string". The dollar sign meant "end of string".

Beginner JavaScript variables

It's my first time using Java Script....
What does this do?
var INTEGER_SINGLE = /\d+/;
What does the forward slashes tell you? How about the backslash? d means for digit?
Thanks!
That creates a regular expression that matches one or more digits.
Anything inside / / is a regular expression. \d matches a digit, and + is the positive closure, which means one or more.
Having said that, depending on what this regex is supposed to do, you may want to change it to:
var INTEGER_SINGLE = /^\d+$/;
^ matches the beginning of the string, and $ the end. The end result would be that any strings you try to match against the regex would have to satisfy it in the string's entirety.
var INTEGER_SINGLE = /^\d+$/;
console.log(INTEGER_SINGLE.test(12)); //true
console.log(INTEGER_SINGLE.test(12.5)); //false
Of course if the regex is supposed to only match a single integer anywhere in the string, then of course it's perfect just the way it is.

Brackets in Regular Expression

I'd like to compare 2 strings with each other, but I got a little problem with the Brackets.
The String I want to seek looks like this:
CAPPL:LOCAL.L_hk[1].vorlauftemp_soll
Quoting those to bracket is seemingly useless.
I tried it with this code
var regex = new RegExp("CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll","gi");
var value = "CAPPL:LOCAL.L_hk[1].vorlauftemp_soll";
regex.test(value);
Somebody who can help me??
It is useless because you're using string. You need to escape the backslashes as well:
var regex = new RegExp("CAPPL:LOCAL.L_hk\\[1\\].vorlauftemp_soll","gi");
Or use a regex literal:
var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi
Unknown escape characters are ignored in JavaScript, so "\[" results in the same string as "[".
In value, you have (1) instead of [1]. So if you expect the regular expression to match and it doesn't, it because of that.
Another problem is that you're using "" in your expression. In order to write regular expression in JavaScript, use /.../g instead of "...".
You may also want to escape the dot in your expression. . means "any character that is not a line break". You, on the other hand, wants the dot to be matched literally: \..
You are generating a regular expression (in which [ is a special character that can be escaped with \) using a string (in which \ is a special character).
var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi;

Javascript split regex question

hello I am trying what I thought would be a rather easy regex in Javascript but is giving me lots of trouble.
I want the ability to split a date via javascript splitting either by a '-','.','/' and ' '.
var date = "02-25-2010";
var myregexp2 = new RegExp("-.");
dateArray = date.split(myregexp2);
What is the correct regex for this any and all help would be great.
You need the put the characters you wish to split on in a character class, which tells the regular expression engine "any of these characters is a match". For your purposes, this would look like:
date.split(/[.,\/ -]/)
Although dashes have special meaning in character classes as a range specifier (ie [a-z] means the same as [abcdefghijklmnopqrstuvwxyz]), if you put it as the last thing in the class it is taken to mean a literal dash and does not need to be escaped.
To explain why your pattern didn't work, /-./ tells the regular expression engine to match a literal dash character followed by any character (dots are wildcard characters in regular expressions). With "02-25-2010", it would split each time "-2" is encountered, because the dash matches and the dot matches "2".
or just (anything but numbers):
date.split(/\D/);
you could just use
date.split(/-/);
or
date.split('-');
Say your string is:
let str = `word1
word2;word3,word4,word5;word7
word8,word9;word10`;
You want to split the string by the following delimiters:
Colon
Semicolon
New line
You could split the string like this:
let rawElements = str.split(new RegExp('[,;\n]', 'g'));
Finally, you may need to trim the elements in the array:
let elements = rawElements.map(element => element.trim());
Then split it on anything but numbers:
date.split(/[^0-9]/);
or just use for date strings 2015-05-20 or 2015.05.20
date.split(/\.|-/);
try this instead
date.split(/\W+/)

Categories

Resources