I am making a schedule application (JS) that needs to both validate an input, and get the various parts of the input. Some sample valid inputs:
8a12p(8am to 12pm)
6a6p (6am to 6pm)
My regex string is currently ^[1-9]|1[0-2][a|p][1-9]|1[0-2][a|p]$, but it doesn't seem to work for all cases (e.g. 8a12x still is valid), and the parts don't seem to split properly in JS. (I need to be able to get the various parts (e.g. [ "8", "a", "12", "p" ] for valid regex.)
Thanks!
Your problem is that you are not using the correct parenthesis.
This works: ^([1-9]|1[0-2])(a|p)([1-9]|1[0-2])(a|p)$
Recomended site for building regex's https://regex101.com/
Explanation on yours
Your regex ^[1-9]|1[0-2][a|p][1-9]|1[0-2][a|p]$ looks for either
^[1-9] or 1[0-2][a|p][1-9] or [1|p]$ and since the first works with your example (since it starts with a number between 1 and 9 the whole regex is true.
Related
I have a simple text field in Retool that I would like to validate includes the substring "-demo" So anything like test-demo, jeremy-demo, etc. would be acceptable. I'm sure with extra thought I could prevent stuff after the -demo part too, but one step at a time..
The closest I've gotten is using the pattern \-demo which is validating only when the string is EXACTLY "-demo" no more no less.
Using their own RegEx Tester (here) I've found /\-demo/g to work, but it does not actually work in practice.
Good morning
(I saw this topic has a LOT of answers but I couldn't find one that fits)
I am writing a little parser in javascript that would cut the text into sections like this :
var tex = "hello this :word is apart"
var parsed = [
"hello",
" ",
"this",
" ",
// ":word" should not be there, neither "word"
" ",
"is",
"apart"
]
the perfect regex for this is :
/((?!:[a-z]+)([ ]+|(?<= |^)[a-z]*(?= |$)))/g
But it has a positive lookbehind that, as I read, was only implemented in javascript in 2018, so I guess many browser compatibility conflicts... and I would like it to have at least a little compatibility...
I considered :
trying capturing groups (?:) but it consumes the space before...
just removing the spaces-check, but ":word" comes in as "word"
parsing the text 2 times, one for words, the other for spaces, but i fear putting them in the right order would be a pain
Understand, I NEED words AND ALL spaces, and to exclude some words.
I am open in other methods, like not using regex.
my last option :
removing the spaces-check and organising my whole regex in the right order, praying that ":word" would be kept in the "special words" group before anything else.
my question :
would that work in javascript, and be reliable ?
I tried
/(((:[a-z]+)|([ ]+)|([a-z]*))/g
in https://regexr.com/ seems to work, will it work in every case ?
You said you're open to non-regex solutions, but I can give you one that includes both. Since you can't rely on lookbehind being supported, then just capture everything and filter out what you don't want, words followed by a colon.
const text = 'hello this :word is apart';
const regex = /(\w+)|(:\w+)|(\s+)/g;
const parsed = text.match(regex).filter(word => !word.includes(':'));
console.log(parsed);
I would use 2 regexes, first one matches the Words, you DON'T want and then replace them with an empty string, this is the simple regex:
/:\w+/g
Then replace with an empty string. Now you have a string, that can be parsed with this regex:
/([ ]+)|([a-z]*)/g
which is a simplified version of your second regex, since forbidden Words are already gone.
I am building a graph drawer and currently working on the math expression parser. I'm done with most parts but I'm stuck at clearing the input text before parsing it. What I'm trying to achieve now is getting rid of unpermitted characters.
For example, in this text:
5ax+4asxxv+sdflog10aloga(132*43)sin(132)
I want to match everything that is not +,-,*,/,^,(,),ln,log,sin,cos,tan,cot,arcsin,arccos,...
and replace them with "".
so that the output is
5x+4xx+log10log(132*43)sin(132)
I need help with the regex.
Spaces don't matter since I clear them out beforehand.
A little bit tricky - at least I couldn't think of a simple way to do what you ask. The regex would get monstrous.
So I did it the other way around - match what you want to keep, and put it back together.
The regex:
[\d+*/^()x-]|ln|log|(?:arc)?(?:sin|cos)|tan|cot
The code:
var re = /[\d+*/^()x-]|ln|log|(?:arc)?(?:sin|cos)|tan|cot/g,
text = '5ax+4asxxv+sdflog10aloga(132*43)sin(132)arccos(1)';
console.log(text.match(re).join(''));
I am currently developing a web-application where I work with java, javascript, html, jquery, etc. and at some point I need to check that whether an input matches a known pattern and only proceed if it is true.
The pattern should be [at least one but max 3 numbers between 0-9]/[exactly 4 numbers between 0-9], so the only acceptable variations should be like
1/2014 or 23/2015 or 123/2016.
and nothing else, and I CANNOT accept something like 1234/3012 or anything else, and this is my problem right here, it accepts everything in which it can find the above pattern, so like from 12345/6789 it accepts and saves 345/6789.
I am a total newbie with regex, so I checked out http://regexr.com and this is the code I have in my javascript:
$.validator.addMethod("hatarozat", function(value, element) {
return (this.optional(element) || /[0-9]{1,3}(?:\/)[0-9]{4}/i.test(value));
}, "Hibás határozat szám!");
So this is my regex: /[0-9]{1,3}(?:\/)[0-9]{4}/i
which I built up using the above website. What could be the problem, or how can I achived what I described? I tried /^[0-9]{1,3}(?:\/)[0-9]{4}$/ibut this doesn't seem to work, please anyone help me, I have everything else done and am getting pretty stressed over something looking so simple yet I cannot solve it. Thank you!
Your last regex with the anchors (^ and $) is a correct regex. What prevents your code from working is this.optional(element) ||. Since this is a static thing, and is probably true, so it does not show any error (as || is an OR condition, if the first is true, the whole returns true, the regex is not checked at all).
So, use
return /^[0-9]{1,3}\/[0-9]{4}$/.test(value);
Note you do not need the (?:...) with \/ as the grouping does not do anything important here and is just redundant. The anchors are important, since you want the whole string to match the pattern (and ^ anchors the regex at the start of the string and $ does that at the end of the string.)
You need use the the following special characters in your regex expression:
^ and $
or \b
so 2 regexp will be correct:
/\b[0-9]{1,3}(?:\/)[0-9]{4}\b/i;
or
/^[0-9]{1,3}(?:\/)[0-9]{4}$/i
I am surprised to not to find any post regarding this, I must be missing something very trivial. I have a small JavaScript function to check if a string matches an object's properties. Simple stuff right? It works easily with all strings except those which contain a forward slash.
"04/08/2015".indexOf('4') // returns 2 :good
"04/08/2015".indexOf('4/') // returns -1 :why?
The same issue appears to be with .search() function as well. I encountered this issue while working on date strings.
Please note that I don't want to use regex based solution for performance reasons. Thanks for your help in advance!
Your string has invisible Unicode characters in it. The "left-to-right mark" (hex 200E) appears around the two slash characters as well as at the beginning and the end of the string.
If you type the code in on your browser console instead of cutting and pasting, you'll see that it works as expected.