Regex to restrict certain characters [duplicate] - javascript

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
is there someone that can help me come up with a regex that restricts these characters !##$%^&*()_-? Most of the post here and google lead me to regex that allow a prescribed set of characters and I cannot seem to have much luck coming up with my own.
Thanks in advance.
EDIT:
After some tries came up with below and seems to work so no further help needed:
"Has restricted-characters_?".replace(/^\!|\#|\#|\$|\%|\^|\&|\*|\‌​(|\)|\_|\-|`|\?/, " ")

rgx = (/[\^!##$%^&*()_?-]/g);
test = str => console.log(str ,str.match(rgx))
test('qwerty')
test('!qwerty')
test('q#werty')
test('qw#erty')
test('qwer$ty')
test('qwert%y')
test('qwerty^')
test('qwertyq&werty')
test('qwertyqwe*rty')
test('qwertyqwert(y')
test('qwertyqwerty)')
test('qwertyqwertyq_wertyqwerty')
test('qwertyqwertyqwe?rtyqwerty')
test('qwertyqwertyqwert-yqwerty')

[^!##$%^&*()_?-]
will do for all the characters. You've got to put them in [ ] with a ^ in front.
Note to escape the - with a \ (Ex: [^!##$%^&*\-()_?]) or put the - in the very end.
This works, you can try it in here,
https://regexr.com/

Related

Remove everything before the first forward slash in the url [duplicate]

This question already has answers here:
How do I parse a URL into hostname and path in javascript?
(26 answers)
Closed 2 years ago.
I need to remove everything before the third forward using regex so that for example https://stackoverflow.com/questions/ask becomes /questions/ask I'm not the greatest when it comes to regular expressions so your help would be much appreciated.
This is what I have so far https://regex101.com/r/qQ2dE4/498
The code I currently have is but want to use regex:
url.substring(url.indexOf('\/') + 3)
Use this:
(?<=.*\/.*\/.*\/).+
Demo
Explanation:
(?<= - its positive look behind, in any position that's pattern inside it is find matching start from this position to forward.
.*\/.*\/.*\/ - it is used inside the positive look behind, it cause matching start after the position that behind that position is 3 forward slashes
.+ - match one or more of from anything
Edit:
From #JaromandaX's comment, this can also be used (and honestly I think it more readable and simper):
(?<=(?:.*?\/){3}).+
I understand the questions asks for regex but the need is unclear - if all you need is the path name there are better tools in the toolbox (for anybody not working on a homework exercise).
const url = 'https://stackoverflow.com/questions/ask'
console.log(new URL(url).pathname)

email regex not working properly with javascript/angular [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I have created a regular expression for email validations.
var regex = "^([a-zA-Z]+([\.-_]?[a-zA-z0-9]+)*)\#([a-zA-Z0-9]+)([-][0-9a-z]+)?\.([a-z-]{2,20})(\.[a-z]{2,3})?$"
To match emails:
1. update#update
2. mohit.bhagat#B-9com.com
3. mohit.Bhagat#us.thalesgroup.com
4. mohit#gmail.com.com.com
If you run this over online website 1 and 4th will fail while 2, 3 will pass.
But when I run this code in Javascript( Browser console ), 1st also passes the validation.
I am using Angular application.
The problem is how JS ignores \.
If you do following
var regex = "[a-z]+#[a-z]+\.[a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+.[a-z]{2,3}"
And if you do this
var regex ="[a-z]+#[a-z]+[\.][a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+[.][a-z]{2,3}"
Pay attention to [.] with this now i was able to get validation error for 1st email.
Complete regex: "^([a-zA-Z]+([-_.]?[a-zA-Z0-9])*)#([a-zA-Z0-9]+([-][a-z0-9A-Z]+)?)[.]([a-z]+[-_]?[a-z]+)([.][a-z]{2,3})?$"
Update:
Or you can do this: var regex ="[a-z]+#[a-z]+\\.[a-z]{2,3}"
As mentioned in comments below, you can do this way to consider . by including it in [.], but its a hack and not origional way to do it.
Best way to have . included in your regex is \.

Regex. Escape group? [duplicate]

This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 5 years ago.
Is there any way to make something.+()[]* matching literally 'something.+()[]*'? I'm using regex builder so manual escaping is not allowed. Sure, i can add hardcoded checks if (char === '+') return '\+' but i'm looking for native solution or better way
UPD
I'm sorry. I forgot to add that matching should be in given order with moving forward but not back. So [+.] will not fit my requirements because it will match both +. and .+. I need only first case (In definition order)
You don't need to escape them if within square brackets.. I just tested and works for me, but maybe not what you are looking for?
something[.+()[]]

Need Help in Regex (Javascript) [duplicate]

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, '')

Using regex to validate in JSP [duplicate]

This question already has answers here:
Validating javascript decimal numbers
(5 answers)
Closed 8 years ago.
I have an input field and I want to validate its content before passing the value further. I'm struggling with creating a proper regex though, as regex created in online editor doesn't really work with my code (actually it blocks everything).
Desired regex behaviour:
Only numbers and ., you can use . only once as a decimal separator, value cannot start/end with ., value cannot start with 0.
My code:
var valueInput = $(row).find("#value").val();
var numbers = /^[0-9.]*$/;
if( !valueInput.match(numbers) ) {
//do something
}
valueInput is fine, it brins the value from the input properly. Any ideas?
Edit.
Thanks for your contribution, I wanted to sum up a little for people from future, looking for an answer:
1. Regex is not a string! I edited former code to make sure nobody's get confused, but comment to get rid of " " was crucial, thanks Pointy.
2. Regex I decided to use is /^([1-9][0-9]*)+(?:\.\d+)?$/. I realized I also need to handle case, when value has only decimal part, so it starts with 0. I didn't really want to play with regex here, so I made a trick: if valueInput start with something other than zero I validate with , else I validate with /^[0]{1,1}\.\d{1,2}$/g.
/[1-9](\d+)?(\.\d+[1-9])?/
This is the best I could come up with. If this doesn't work, do let me know.
P.S. I find regexr a nice place to test regex. Cheers.
I'd do this:
valueInput.match(/([1-9]d*)+(?:\.\d+)?$/)

Categories

Resources