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

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.

Related

Is there a best practice to define long strings in JavaScript considering column width? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I sometimes run into situations where I would need a best practice to define a long string. I'm talking about something like this:
const text = 'This is indeed a very long string. Some might say that it is really, really long.'
My problem here is that the string is just too wide. If I would prefer a solution where the column width is considered. I would usually use one of two solutions:
a.)
const text = 'This is indeed a very long string. ' +
'Some might say that it is really, really long.'
The problem with this one is that it uses an unnecessary concatenation.
b.)
const text = `This is indeed a very long string.
Some might say that it is really, really long.`
And the problem with this one is that the resulting string will actually contain a new line, which might not be wanted in some situations.
I realize that this might be a question for opinionated answers, but I still feel like that I'm missing something, or that there is a better solution out there. Please show me if you have one!
You can use the string continuation character \ (single backslash) to do that.
const longString = "This is a really really \
long string that should \
not be split in multiple lines."
console.log(longString)
See the documentation in the Long literal strings section for details.

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.

Require hyphens using javascript regex [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 keep seeing regex on phone numbers that allows the number as all one string, and does not make someone enter the hyphen. Using these:
/^[-\s]$/
/^(-\s)$/
/^[-]$/
/^(-)$/
/^([-]\s)$/
as a regex allows spaces or no hyphen to be typed at all. How do you require a hyphen to be inserted?
EDIT: There shouldn't be downvotes that claim "you shouldn't be forcing someone to use this!". This is the client's requirement, and if you ever had to read phone numbers that were 1234567890 instead of 123-456-7890, that would enable you to see an area code and phone exchange at a glance, I would think anyone would want this.
And not that it should matter to the question in any way because it was kept vague and specific on purpose, but this is for a textbox that will not require any non-NANP numbers. I did that on purpose so we can focus on how to require hyphens, not reinvent the wheel on phone number regexs.
This simple regex will do the job:
^\d{3}-\d{3}-\d{4}$

Javascript http regex [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 8 years ago.
Improve this question
I am currently using the following to convert [url=][/url] to a html link:
s = message.replace(/\[url=([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='$1'>$2</a>")
That work's fine.
I then added on another replace function using a regex to replace www with http://www like so:
s = message.replace(/\[url=([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='$1'>$2</a>")
.replace(/www/g, "http://www");
This is probably not the best/efficient method and also does not support https:// which is not a priority at the moment but is something I would like to include at some point. Could someone please advise me on what I could do to improve the regex?
Couple of things:
First, there are some problems with your second pattern. It is not case insensitive (whereas the first pattern is) so it won't catch things like 'WWW'. Maybe that's desirable, maybe not. But it's also global, and is not anchored to the beginning of the URL. So it will replace www anywhere in the URL. Changing it to something like /href=\'www/ and then changing the replace string to href='http://www should solve these problems.
Secondly, in a case like this, using 2 regular expressions may not be bad. You can fold it into one regex if you want, but that doesn't mean it is any more efficient for the computer or the poor human who happens to be reading it.
All that said, one way to accomplish this with a single regular expression, is to do something like:
s = message.replace(/\[url=(?:http:\/\/)?([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='http://$1'>$2</a>")
This may accomplish what you want. It does not key off of "www", though, it simply prepends "http://" to any URL that does not already begin with that string. It also doesn't support https, as you mentioned, but I'm not sure how you will support that anyway. If all you are given is a URL with no protocol, how do you determine whether or not to make it https?

Categories

Resources