This question already has an answer here:
javascript regex: matching a phone number
(1 answer)
Closed 7 years ago.
Before submitting the form, I am having values like (33) 3333-3333 and (333) 333-3333. I need to check these kind of values in regex before submitting the form.
If the value is in the above format, I have to remove the special characters, whitespace and have it as numbers alone: 3333333333
Please help me in writing a regex in javascript.
<input type="tel" />
Job done. Specs
I can suggest you to use a regex like this:
/^\((\d{2,3})\) ?(\d{3,4})-(\d{4})$/
And using substitutions like $1$2$3
[Regex Demo]
Use this regex:-
str.replace(/[^0-9]/g, '')
Related
This question already has an answer here:
Convert JavaScript Regex to C#
(1 answer)
Closed 2 years ago.
How can I convert to following Js regex into C#
let regex = /^([+]?\d{1,2}[.-\s]?)?(\d{3}[.-]?){2}\d{4}$/;
This patterns matches with following international mobile phone numbers
044668180099,
+49-691-234-5678,
+90-537-325-2345,
90-537-566-7152,
I want to do same matches wirh C#
Please advice
Remove '/' at the start and the end of the string.
Regex.Match(yourline, #"^([+]?\d{1,2}[.\-\s]?)?(\d{3}[.-]?){2}\d{4}$");
See also this page about Regex.Match() from the microsoft docs for more overloads.
This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 7 years ago.
I have this regex in Ruby: http://rubular.com/r/eu9LOQxfTj
/<sometag>(.*?)<\/sometag>/im
And it successfully matches input like this:
<sometag>
123
456
</sometag>
Which would return
123
456
However, when I try this in javascript (testing in chrome), it doesn't match anything.
Does javascript's multiline flag mean something else?
I want to capture everything non-greedily between two given tags.
How can I accomplish this in javascript using regex? Here is a Debuggex Demo
<sometag>(.*?)<\/sometag>
This is not XML parsing.
Javascript does not support multiline expressions when using . alone. You have to use [\s\S] in place of . so an example that satisfies what you want would be:
var x = "<sometag>\n\
123\n\
456\n\
</sometag>";
var ans = x.match(/<sometag>([\s\S]*?)<\/sometag>/im).pop();
// ans equals " 123 456"
note that you still need the m modifier.
This question already has answers here:
How to check if a string is a legal "dd/mm/yyyy" date?
(7 answers)
Closed 8 years ago.
I need your help,
Is there a way to code a javascript function that would regex and test a string or using any other means to check if a date string already has the two forward slashes in it? ie. dd/mm/yyyy
regex('04/07/2014') { return true }
Thanks in advance for all your help?
You can use a regex test:
/\/.*\//.test("04/07/2014")
You can use this regex:
/([^/]*\/){2}/
to check if input has at least 2 forward slashes.
use /\/.*\//.test( str ) it returns true if str has two forward slashes in it.
This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 9 years ago.
I have no experience with regular expressions in java script, but I need to derive a pattern for
FMLast1234#ung.edu. There only needs to be a pattern for FMLast1234 because #ung.edu needs to remain the same. I am not sure if I just do this \b[a-z][a-z][a-z][0-9] or what.
Does there need to be a range for each character in the pattern? I need to check for variations of the pattern FMLast1234 not just a random assortment of characters and numbers.
/[a-zA-Z0-9]#ung.edu/.test("123#ung.edu") or
if(emailString.split('#')[1] === "ung.edu")
Edit : As per plalx comment here is my answer
/^\w+#ung.edu$/.test("aaa123#ung.edu")
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I'm trying to use RegEx to select all strings between two dollar signs.
text = text.replace(/\$.*\$/g, "meow");
I'm trying to turn all text between two dollar signs into "meow" (placeholder).
EDIT:
Original question changed because the solution was too localized, but the accepted answer is useful information.
That's pretty close to what you want, but it will fail if you have multiple pairs of $text$ in your string. If you make your .* repeater lazy, it will fix that. E.g.,
text = text.replace(/\$.*?\$/g, "meow");
I see one problem: if you have more than one "template" like
aasdasdsadsdsa $a$ dasdasdsd $b$ asdasdasdsa
your regular expression will consider '$a$ dasdasdsd $b$' as a text between two dolar signals. you can use a less specific regular expression like
/\$[^$]*\$/g
to consider two strings in this example