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 6 years ago.
Improve this question
I am going nuts on this for a few days now. I have read and tried many previously answered questions close to this topic, I've fiddled with all kinds of expressions on regexr.com (very awesome page BTW) but I can't for the life of me figure it out.
I want to do the following in JavaScript/Jquery (this has to do with contenteditable="true" elements where I want to restrict user input).
Some elements are for text input without spaces and allowed numbers (abc1_d-ef), some for free text (abc. d2ef, gh6i? j8kl: mno!), some for integers (123), some for decimals (1,2 / 1.2).
BUT I want to always forbid a newline character or tab (\n \r \t \f).
So:
var pattern = new RegExp(.........);
var text = $("#my-id").html();
var test = pattern.test(text);
// test should be true for correct text / integer / decimal
// but should be false as soon as text contains a newline, tab etc.
So basically I'm looking for 4 different expressions:
letters, numbers, underscore, hyphen
letters, numbers, spaces, special characters and such
numbers 0-9
numbers, comma and dot
but none of them with newline etc.
I hope I could make myself clear.
For all your requirements, you can use:
^[-,.\w ]+$
See a demo on regex101.com.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I am trying to figure how I can search for a pattern that gives me only digits or digits followed by only one letter. I know I can use /\D\g to find only digits but I dont know how to find digits with only one letter after it. The letters can only be the following:['a','A','b','B','c','C','d','D','n','N','e','E','s','S','w','W']
const testPattern = /[A-Za-z][0-9]/
console.log('item_10a_object10a'.pattern(testPattern))
First, you need to group the whole thing with (), and end with /g so it matches multiple groups.
If you want digits first then a number, you need to put the letter block after the numbers: ([0-9][A-Za-z])
If you want multiple digits to match, you need a + after the numbers block: [0-9]+
All together: /([0-9]+[A-Za-z])/g
For reference, \d does the same thing as [0-9], so you could do /(\d+[A-Za-z])/g instead
LPT: use regex101 to build and test regex queries
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 need validate a input using regex (i think it's better) but in this input can be one or more "sentences" and be [A-Z] size 1. How can i do that?
E.g.:
A,B,D,G,J,X no repeat letters but this validate i do in code. I think regex is better 'cause validate a entire sentence instead letter by letter using a loop and split. My english is rusty, appreciate some help to improve =)
Note, Assumption is you want a single letter
If you just want to validate:
if (/([A-Z]*)?,([A-Z]*),?/.test(subject)) {
// Successful match
} else {
// Match attempt failed
}
If you are using to get extract values:
result = subject.match(/([A-Z]*)?,([A-Z]*),?/g);
Maybe this can help you ([A-Z],)+[A-Z] it will match a serie of uppercase letter followed by comma, and end with uppercase letter :
regex demo
A,B,D,G,J,X -> matches
A,B,DE,G,J,X -> not matches
A,B,D,G,J,XY -> not matches
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 am trying to use regular expression to check whether a password field contains more than two special characters in it.Is it possible to perform this check using regular expression in javascript?If so how?
I think you mean the special characters as _ or any non-word character. The below regex would match the strings which has more than two (atleast three) special characters.
^.*?[\W_].*?[\W_].*[\W_].*$
Example:
> /^.*?[\W_].*?[\W_].*[\W_].*$/.test("foo_'bar")
false
> /^.*?[\W_].*?[\W_].*[\W_].*$/.test("foo_'ba:r")
true
> /^.*?[\W_].*?[\W_].*[\W_].*$/.test("foo_'ba:r{}{}[]")
true
If your string matches the regex: /^(?:.*[!*$|#]){3}/ it means that there're 3 or times one of the special characters contained in the character class.
It's up to you to define tthe special characters to include in this character class
x{2,} 2 or more of x
Is probably what you are looking for
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 9 years ago.
Improve this question
I have a following requirement to only allow capital letters and , in a javascript form . I am unsure on how to check for special characters and script tags . I have written the following code . I do not want to allow characters such as $,%,& etc .
var upperCase= new RegExp('[A-Z]');
var lowerCase= new RegExp('^[a-z]');
var numbers = new RegExp('^[0-9]');
if($(this).val().match(upperCase) && $(this).val().match(lowerCase) && $(this).val().match(numbers))
{
$("#passwordErrorMsg").html("OK")
}
Based on what you've given us, this may suit the bill. It will determine if any characters are not in character classes a-z, A-Z or 0-9 but note this will also treat é or similar characters as rejected symbols.
So if the value is 'test_' or 'test a' it will fail but it will pass for 'testa'. If you want it to accept spaces change the regex to /[^a-zA-Z0-9 ]/.
if(!/[^a-zA-Z0-9]/.test($(this).val())) {
$("#passwordErrorMsg").html("OK");
}
This may be helpful.
javascript regexp remove all special characters
if the only characters you want are numbers, letters, and ',' then you just need to whitespice all characters that are not those
$(this).val().replace(/[^\w\s\][^,]/gi, '')
This link may be helpful:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
It has a lot of information on JS Regexps.
For a dollar sign ($), the regexp syntax would be: \$. You need to escape the special character so it is read as a literal. The syntax would be the same for the other special characters, I believe.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my website , i need a REGEXP in java script for validation TITLE which can take alphabates, digits and Special char set [, / ( ) & - : . space], but if any user enter only single and double spaces or single or double .. like [..] in title or double digit [1 2] then it's should not allowed, atlest one aplhabate is required. please help
You can use this pattern:
^[-a-z0-9,/()&:. ]*[a-z][-a-z0-9,/()&:. ]*$
This will match any number of your special characters followed by a Latin letter, followed by number of your special characters. It's effectively equivalent to [-a-z0-9,/()&:. ]+ except it requires at least one [a-z] somewhere in the string.
Of course, you need to escape the \ when written as a regex literal in javascript, and you probably want to use the i flag for case-insensitive matching:
var pattern = /^[-a-z0-9,\/()&:. ]*[a-z][-a-z0-9,\/()&:. ]*$/i