Regex to accept alphanumberic, whitespace and - in Javascript? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What would be the Regex to accept alphanumeric, whitespace and - in Javascript.

"HI THERE 0-9 are valid characters".match(/[a-z0-9\s-]+/i)
a-z will match your favourite alphabet (the roman one!)
0-9 will match your favourite numerals (the arabic ones!)
\s will (probably) match your favourite whitespace characters (\f, \n, \r, \t, \v, \u00A0, \u2028, and \u2029)
-, since it's at the end of the set, will match a literal -.
The i modifier at the end of the regex makes it case-insensitive, so a-z will also match A-Z.

Related

Regex matching /xx/xx/ or /xx/xx - javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
What is the regular exression which will match the string /xx/xx/ as well as /xx/xx. xx can be any two letters.
The simpelest solution would be a literal pattern of "slash, 2 chars, slash, 2 chars, optional slash":
/\/\w\w\/\w\w\/?/
Structure:
/ //JS's "Start regex"
\/ // Escaped "Slash" (matches / )
\w // Any word character ([A-Za-z0-9_]) (Twice)
// Then a slash and 2 \w's
\/? // Optional slash.
Usage:
"/xx/xx/".match(/\/\w\w\/\w\w\/?/) // ["/xx/xx/"]
"/xx/xx".match(/\/\w\w\/\w\w\/?/) // ["/xx/xx"]
"/xx/x".match(/\/\w\w\/\w\w\/?/) // null
You could make the regex a little shorter:
/(\/\w\w){2}\/?/
Also, you may want to replace \w with [A-z], since \w also matches digits ([0-9]) and the underscore character (_). If you want the regex to only allow letters, use [A-z].
Hope below would help:
\/[a-zA-Z][A-Za-z]\/[A-Za-z][A-Za-z](\/)?/
Assuming x could be caps or small letters.
Test Samples:
Ab/cd --> True
AA/BB --> True
A/B --> False
XX/SS/ --> True
/X/x/ --> False

Regex to not allow special characters (Javascript) [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 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.

Regular Expression in Java Script [closed]

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
I am not able to understand this pattern. What does this Regex mean?
/([^0-9])\d{1,4}([^0-9])/g
This regex will:
/ /* delimeter */
([^0-9]) /* capture non-digit character */
\d{1,4} /* match 1 to 4 digits */
([^0-9]) /* capture 1 non-digit character */
/g /* multiple times in the string */
PS: [^0-9] is identical to [^\d] also identical to \D
It's matching a String which contains the following:
some character which is not a digit
followed by 1 to 4 digits
followed by a character which is not a digit

Regular Expression for SKU validation [closed]

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 8 years ago.
Improve this question
I do not know if this is nonsense but I need a regular expression to validate the SKU of a product, taking into account that it must be 10 characters minimum and 20 maximum and allow [0-9] and [a-zA-Z] but I have not the slightest idea, the regular expression is to be used from Javascript with . test(), any help?
You can use character class and specify length range for this.
A character class is a set of characters (or character range) enclosed in a pair of square brackets like [abc] or [A-Z].
So for your case, it would be [A-Za-z\d] which denotes an upper case or lower case letter or a digit. \d stands for digit.
For allowing n occurrence of any pattern, you can use <your_pattern>{n}. If it is a range from m to n, use <your_pattern>{m,n}. For specifying only minimum, use <your_pattern>{n,} and for specifying only maximum, use <your_pattern>{,n}.
Using a service for regex is helpful.
This should be what you need
^[a-z0-9A-Z]{10,20}$
if it is just JavaScript you can set a flag for upper and lower case
/^[a-z0-9]{10,20}$/i
Example below.
http://regex101.com/r/uE8fH1

TITLE VALIDATION in JavaScript for REGEXP which can take alphanumeric and can take Special char [, / ( ) & - : . space] [closed]

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

Categories