Where are javascript's regular expressions (regex) used? [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
Are they useful to know? How are they used?

Regular expressions are not a JavaScript-specific feature and are typically used to check for faults in use input, for example to notify users who forget the # or domain part of an email address or too few/many digits in a phone number. For more examples, look over there.

Email address validation

JavaScript is like Perl, has native regular expression notation.
Regular expressions are used to match texts. For example, validating age:
if (field.value.match(/^\s*\d+\s*$/) === null) alert("error: invalid age.");
Read this for a documentation: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
and JS form validation tutorial:
http://www.devarticles.com/c/a/JavaScript/Form-Validation-with-JavaScript-Regular-Expressions-Part-1/

Related

JavaScript RegExp to match a given string only if it is not surrounded by brackets [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 5 years ago.
Improve this question
I am looking for a JavaScript regular expression that would match the word "foo" in a phrase "This <is foo" or "This is foo" or "This is foo>" but would not match in the phrase "This <is foo>". I know that in other languages this is solved by using negative lookbehinds, but AFAIK, JavaScript does not support these. I have come across similar questions, but none of them matched exactly what I am looking for, and I can't think of a solution on my own. I would be grateful for any hint.
I am testing my attempts here.
You can match it as group not bounded by the opening and closing brackets.
[^<].*(foo).*[^>]

Convert JavaScript regular expression to java [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 5 years ago.
Improve this question
I am trying to convert JavaScript regular expression to java regular expression. here is my current javascript regular expression that I am trying to convert :
var re = /\a(?![^<]*>)/g;
I have searched and found out that I need to change the back-slash / into double back-slashes // and start with "
but what about /g at the end? do we need to change it too?
Thank you
The equivalent regex is:
string regex = "\\a(?![^<]*>)";
There is no equivalent of the g flag in JAVA, you have to use replaceall() instead of replace() if you want to replace or use a matcher and a while loop (see How can I find all matches to a regular expression in android).

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

How do you implement an html syntax highlighter in JavaScript? [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 9 years ago.
Improve this question
Can i use regex? if yes, how do i replace every match with a span tag? if no what can i use? a dictionary with all possible html tags? This is for solely educational purposes so please don't refer me to a JavaScript library.
It's similar to how you would do syntax highlighting in any other language. Typically, you would implement a tokenizer which breaks the html snippet into individual tokens (e.g. using regular expressions), and then you would apply styling rules to the tokens.
I would look at Prism. Its code is pretty simple. The core is here:
https://github.com/LeaVerou/prism/blob/gh-pages/components/prism-core.js
This contains the tokenization and highlighting functions. The regular expressions for HTML/XML are in this file:
https://github.com/LeaVerou/prism/blob/gh-pages/components/prism-markup.js

How can I prune out all the unwanted chars from a string in one batch? [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 have a mac address: "22:33:44:12:34:56" and I want to prune out all the semicolons.
If I do it without regular expressions it would be :
"22:33:44:12:34:56".replace(":","")
then "2233:44:12:34:56".replace(":","") until I finish with all colons.
What is the regular expression that does this in a single batch?
Regex flag g makes the global search:
"2233:44:12:34:56".replace(/:/g, "");
REF: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp
Do this :
"22:33:44:12:34:56".replace(/:/g,"")
The MDN explains how you can use replace with a regex, and especially the g flag which makes the function replace all occurrences and not just the first one.
How about this?
'22:33:44:12:34:56'.split(':').join('')

Categories

Resources