My simple Regex doesn't work [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have this simple regex that is supposed to match any numbers and '+' signs
/^[\d\+]+$/g
What it does is this:
1 => true
11 => false
11+11 => true
1+1 => false
It's driving me nuts!
(I'm using JavaScript, if it matters)

Some assumptions I did when reproducing your error:
You're using the test()-method of the RegExp-prototype, not the match()-method of the String-prototype.
Your pattern is stored in a variable and you reuse it over multiple calls to the test()-method.
At a first glance, the result is somewhat unexpected, but I'll try to explain what is happening.
Your RegExp has the global-Flag set to true. This causes subsequent calls to the test()-method to advance past previous matches, as stated here. This essentially means that after your first regular expression is evaluated and a match was found, the index of this match is stored into the RegExp-object and the next match will start at that very index, omitting some characters at the beginning. For a deeper explanation, I'd recommend reading this thread.
This is not really what you want, right? My quick recommendation would be to simply remove the global-flag, as you don't really need it from my point of view. If you want to ensure that your regular expression is only matching full strings rather than substrings, use the ^and $ metacharacters (as you already did).
EDIT:
If you really need the global-flag though, try to use the match()-method of the String-prototype, as it does not advance past previous matches. Instead it uses the advancing feature and captures all matches, resetting the index afterwards.
var pattern = /^[\d\+]+$/g;
"1".match(pattern); // => true
"11+11".match(pattern); // => true
"1+1abc".match(pattern); // => false

Related

How to pass a single quote around a javascript variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I have a javascript code as shown below in which I want to pass a single quote around a javascript variable attribute.
Problem Statement:
I am wondering what changes I need to make at Line A so that I am able to pass a javascript variable in a single quote.
At Line A, I did this `'${attribute}'` but I am getting an error Uncaught SyntaxError: missing ) after argument list.
It looks like you're trying to use a template literal, but have over-complicated it:
confirm(`Are you sure you want to delete '${attribute}' ?`)
Note the use of back-ticks around the whole template literal, which syntactically allows you to use any quotes you like within the literal itself.
You need to put the whole string inside backticks and then you don't have to 'escape' your single quotes anymore.
confirm(`Are you sure you want to delete '${attribute}' ?`)

Is it possible to make a regex that accepts ANY substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm somewhat new to regex. I understand most of the basics but what I'm trying to do is beyond my knowledge, and may not even be possible.
I'm trying to make a regex in JavaScript that can match a series of function calls in the following pattern.
Name.Name(Params).Name(Params)
The names could be any standard java function name. I understand how do to this part. The params though can be different number of parameters (Currently only 0-2)
My biggest issue however is that params could potentially take ANY string with either a single or double quotation mark, or variable names. I have added some examples below as I need all of these to work with my regular expression (if Possible).
Examples:
Func.Foo().Bar()
Foo.Bar('foo', bar).Foobar()
Foo.Bar("foo", "bar").bar(')')
Foo.Bar('/"foo/"').bar("foo(bar/")")
My main concern here is I cant just look for a opening and parentheses or even 2 quotation marks.
Is it possible to use a regex so that I can parse the function call and parameters out?
The short answer to the Question in the title is yes, you can build a regex that matches any substring. But unfortunately that is not what you want. If you allow arbitrary substrings your regex will either match many cases you dont want to match or it will become extremely complex (see the email regex for an example).
What you want is a tokenizer!(https://medium.freecodecamp.org/how-to-build-a-math-expression-tokenizer-using-javascript-3638d4e5fbe9)
Edit: for the solutions in the comments: the ast parser is for java, the author wants to use javascript.

I need a Javascript REGEX for Integers between 18 and 140 inclusive [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm still trying to find my way around Javascript regular expressions, and I have not been able to deduce how to write this regex I need.
Please I need a Javascript regex basically for age. I want to allow anybody between ages 18 and 140. With 18 being allowed and 140 being allowed. I want a regex that basically accepts 18,19,20,21,...138,139,140. and ONLY THIS.
I'd appreciate any help.
P.S. I hope I just get a simple answer without Stack Overflow closing the question down and saying 'duplicate' or this or that... if not this might be my very last question on stack overflow, because sadly, it SEEMS like Stack Overflow is making it harder to ask a simple question. The point they want you to do a TON OF RESEARCH...or at least a lot, before you ask a question. Even though many times we ask questions precisely because we DON'T have the the time to do a lot of research. :: sigh ::
When Answering your question, I am not the person to ask why you need regex for this.
And the regex you want is
/^(1[89]|[2-9][0-9]|1([0-3][0-9]|40))$/
Sample
var age=/^(1[89]|[2-9][0-9]|1([0-3][0-9]|40))$/;
console.log(age.test(18));
console.log(age.test(140));
console.log(age.test(12));
console.log(age.test(142));
But, you can simply use the following code to test
if(age>=18 && age<=140)
That is
function test(age){
return age>=18 && age<=140;
}
console.log(test(18));
console.log(test(140));
console.log(test(12));
console.log(test(142));
Try this:
/^(1[89]|[2-9]\d|1[0-3]\d|140)$/
Let me know if that does what you need.

What dose it mean 'regex' on javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I found this too code in one example script:
'regex':'^[a-zA-Z]{2} *\\d{6}|[a-zA-Z]{2} *\\d{3} *[a-zA-Z]{2}|[a-zA-Z]{2}[a-zA-Z]{2}[0-9]{6}|[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{5}|[a-zA-Z]{2}[a-zA-Z]{1}[0-9]{5}|[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{1}[0-9]{4}|[a-zA-Z]{2}[0-9]{5}[a-zA-Z]{1}|[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{1}[0-9]{3}$',
but i really do not know what it means...
Regex is a regular expression for example disecting parts of text from a bigger collection of text.
Say for example that you want to find all names in a newspaper. Instead of reading the entire thing looking for each name you can make a regex model of finding every word starting with a capital letter that is not right after a punctuation.
In your example the regex ^ means it looks for something that starts with the following:
- A small or capital letter between a to z
- The word is to letters long
Read a couple of examples and you'll get the hang of it.
http://www.dreambank.net/regex.html
It is simply just a model how text is built up.
If you just want the explanation of the posted regex, you should try regex101 . It breaks down the posted regex into capture groups and gives a pretty detailed explanation and matches for a given input and regex.
Like many have already suggested, it would be better if you start by reading about how regex works. I'm sure you'll find plenty of related questions on Stackoverflow.
However, I have created a simple demo of your posted regex on regex101 like I said. I'm going to refrain from posting the entire explanation here. it would be a good exercise if you try to read the explanation and understand it on your own.

filter invalid mobile numbers with Javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to write in java script to exclude invalid mobile numbers that are entered into the SQL database. i currently have the regex function within my script however it cant pick up brackets(), resulting in numbers such as (123) 456789 not being included.
Is there any extension to the regex i could use to include brackets?
There is a free google API that can validate numbers for you, even tell you the country code etc.
https://code.google.com/p/libphonenumber/
Never tried it, but I evaluated it once for another project I worked on.
This is Java, not JS, but still you may consider moving your validation logic to a Java servlet and invoke using an AJAX call.
1) This would expect atleast one white space character after parenthesis
/\(\d{3}\)\s+\d{6}/
Or
/\(\d+\)\s+\d+/ //in case of digit not specified.
2) This would match with or without space
/\(\d{3}\)\s*\d{6}/
//eg:-
// (123)456789
// (123) 456789

Categories

Resources