how to check digits present in javascript string or not - javascript

I need a regex which check the string contains only A-Z, a-z and special characters but not digits i.e. (0-9).
Any help is appreciated.

You can try with this regex:
^[^\d]*$
And sample:
var str = 'test123';
if ( str.match(/^[^\d]*$/) ) {
alert('matches');
}

Simple:
/^\D*$/
It means, any number of not-a-digit characters. See it in action…
The alternative is to reverse your test. Just check if there's a digit present, using the trivial:
/\d/
…and if that matches, your string fails.

You're looking for a character class: ^[A-Za-z.,!##$%^&*()=+_-]+$.
The ^ and $ anchor the regex by marching the beginning and end of the string, respectively.

what about:
var re = /^[a-zA-Z!#$%]+$/;
Fell free to add any special character you need inside the character class

Related

Regex catch from the hash sign "#" to the next white space

I have a script line this :
#type1 this is the text of the note
I've tried this bu didn't workout for me :
^\#([^\s]+)
I watch to catch type in other words I to get whats between the hash sign "#" and the next white space, excluding the hash "#" sign, and the string that I want to catch is alphanumeric string.
With the regex functionality provided by Javascript:
exec_result = /#(\w*)/.exec('#whatever string comes here');
I believe exec_result[1] should be the string you want.
The return value of exec() method could be found over here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
You're really close:
/^\#(\w+)\s/
The \w matches any letters or numbers (and underscores too). And the space should be outside the matching group since I guess you don't want to capture it.
To get an alphanumeric match (which will get you type1), instead of the negated character class [^\s] which matches not a whitespace character, you could use a character class and specify what you want to match like [A-Za-z0-9].
Then use a negative lookahead to assert what is on the right is not a non-whitespace char:
^#([A-Za-z0-9]+)(?!\S)
Regex demo
Your match is in the first capturing group. Note that you don't have to escape the \#
For example using the case insensitive flag /i
const regex = /^#([A-Za-z0-9]+)(?!\S)/i;
const str = `#type1 this is the text of the note`;
console.log(str.match(regex)[1]);
If you only want to match type, you might use:
^#([a-z]+)[a-z0-9]*(?!\S)
Regex demo
const regex = /^#([a-z]+)[a-z0-9]*(?!\S)/i;
const str = `#type1 this is the text of the note`;
console.log(str.match(regex)[1]);
I've figured it out.
/^\#([^\s]+)+(.*)$/

javascript regex for special characters

I'm trying to create a validation for a password field which allows only the a-zA-Z0-9 characters and .!##$%^&*()_+-=
I can't seem to get the hang of it.
What's the difference when using regex = /a-zA-Z0-9/g and regex = /[a-zA-Z0-9]/ and which chars from .!##$%^&*()_+-= are needed to be escaped?
What I've tried up to now is:
var regex = /a-zA-Z0-9!##\$%\^\&*\)\(+=._-/g
but with no success
var regex = /^[a-zA-Z0-9!##\$%\^\&*\)\(+=._-]+$/g
Should work
Also may want to have a minimum length i.e. 6 characters
var regex = /^[a-zA-Z0-9!##\$%\^\&*\)\(+=._-]{6,}$/g
a sleaker way to match special chars:
/\W|_/g
\W Matches any character that is not a word character (alphanumeric & underscore).
Underscore is considered a special character so
add boolean to either match a special character or _
What's the difference?
/[a-zA-Z0-9]/ is a character class which matches one character that is inside the class. It consists of three ranges.
/a-zA-Z0-9/ does mean the literal sequence of those 9 characters.
Which chars from .!##$%^&*()_+-= are needed to be escaped?
Inside a character class, only the minus (if not at the end) and the circumflex (if at the beginning). Outside of a charclass, .$^*+() have a special meaning and need to be escaped to match literally.
allows only the a-zA-Z0-9 characters and .!##$%^&*()_+-=
Put them in a character class then, let them repeat and require to match the whole string with them by anchors:
var regex = /^[a-zA-Z0-9!##$%\^&*)(+=._-]*$/
You can be specific by testing for not valid characters. This will return true for anything not alphanumeric and space:
var specials = /[^A-Za-z 0-9]/g;
return specials.test(input.val());
Complete set of special characters:
/[\!\#\#\$\%\^\&\*\)\(\+\=\.\<\>\{\}\[\]\:\;\'\"\|\~\`\_\-]/g
To answer your question:
var regular_expression = /^[A-Za-z0-9\!\#\#\$\%\^\&\*\)\(+\=\._-]+$/g
How about this:-
var regularExpression = /^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{6,}$/;
It will allow a minimum of 6 characters including numbers, alphabets, and special characters
There are some issue with above written Regex.
This works perfectly.
^[a-zA-Z\d\-_.,\s]+$
Only allowed special characters are included here and can be extended after comma.
// Regex for special symbols
var regex_symbols= /[-!$%^&*()_+|~=`{}\[\]:\/;<>?,.##]/;
This regex works well for me to validate password:
/[ !"#$%&'()*+,-./:;<=>?#[\\\]^_`{|}~]/
This list of special characters (including white space and punctuation) was taken from here: https://www.owasp.org/index.php/Password_special_characters. It was changed a bit, cause backslash ('\') and closing bracket (']') had to be escaped for proper work of the regex. That's why two additional backslash characters were added.
Regex for minimum 8 char, one alpha, one numeric and one special char:
/^(?=.*[A-Za-z])(?=.*\d)(?=.*[!##$%^&*])[A-Za-z\d!##$%^&*]{8,}$/
this is the actual regex only match:
/[-!$%^&*()_+|~=`{}[:;<>?,.##\]]/g
You can use this to find and replace any special characters like in Worpress's slug
const regex = /[`~!##$%^&*()-_+{}[\]\\|,.//?;':"]/g
let slug = label.replace(regex, '')
function nameInput(limitField)
{
//LimitFile here is a text input and this function is passed to the text
onInput
var inputString = limitField.value;
// here we capture all illegal chars by adding a ^ inside the class,
// And overwrite them with "".
var newStr = inputString.replace(/[^a-zA-Z-\-\']/g, "");
limitField.value = newStr;
}
This function only allows alphabets, both lower case and upper case and - and ' characters. May help you build yours.
This works for me in React Native:
[~_!##$%^&*()\\[\\],.?":;{}|<>=+()-\\s\\/`\'\]
Here's my reference for the list of special characters:
https://owasp.org/www-community/password-special-characters
If we need to allow only number and symbols (- and .) then we can use the following pattern
const filterParams = {
allowedCharPattern: '\\d\\-\\.', // declaring regex pattern
numberParser: text => {
return text == null ? null : parseFloat(text)
}
}

Regular expression for not allowing spaces in the input field

I have a username field in my form. I want to not allow spaces anywhere in the string. I have used this regex:
var regexp = /^\S/;
This works for me if there are spaces between the characters. That is if username is ABC DEF. It doesn't work if a space is in the beginning, e.g. <space><space>ABC. What should the regex be?
While you have specified the start anchor and the first letter, you have not done anything for the rest of the string. You seem to want repetition of that character class until the end of the string:
var regexp = /^\S*$/; // a string consisting only of non-whitespaces
Use + plus sign (Match one or more of the previous items),
var regexp = /^\S+$/
If you're using some plugin which takes string and use construct Regex to create Regex Object i:e new RegExp()
Than Below string will work
'^\\S*$'
It's same regex #Bergi mentioned just the string version for new RegExp constructor
This will help to find the spaces in the beginning, middle and ending:
var regexp = /\s/g
This one will only match the input field or string if there are no spaces. If there are any spaces, it will not match at all.
/^([A-z0-9!##$%^&*().,<>{}[\]<>?_=+\-|;:\'\"\/])*[^\s]\1*$/
Matches from the beginning of the line to the end. Accepts alphanumeric characters, numbers, and most special characters.
If you want just alphanumeric characters then change what is in the [] like so:
/^([A-z])*[^\s]\1*$/

JavaScript match ( followed by a digit

In string n+n(n+n), where n stands for any number or digit, I'd like to match ( and replace it by *(, but only if it is followed by a number or digit.
Examples:
I'd like to change 2+22(2+2) into 2+22*(2+2),
I'd like to change -1(3) into -1*(3),
4+(5/6) should stay as it is.
This is what I have:
var str = '2+2(2+2)'.replace(/^[0-9]\(/g, '*(');
But it doesn't work. Thanks in advance.
Remove the ^, and group the digits:
'2+2(2+2)'.replace(/([0-9])\(/g, '$1*(')
'2+2(2+2)'.replace(/(\d)\(/g, '$1*(') //Another option: [0-9] = \d
Suggestion: 2. is often a valid number (= 2). The following RegExp removes a dot between a number and a parenthesis.
'2+2(2+2)'.replace(/(\d\).?\(/g, '$1*(') //2.(2+2) = 2*(2+2)
Parentheses create a group, which can be referenced using $n, where n is the index of the group: $1.
You started your RegExp with a ^..., which means: Match a part of the string which starts with .... This behaviour was certainly not intended.
var str = '2+2(2+2)+3(1+2)+2(-1/2)'.replace(/([0-9])\(/g, '$1*(');
http://jsfiddle.net/ZXU4Y/3/
This follows what you wrote (the bracket must follow a number).
So 4( will be changed to 4*( it could be important for example for 4(-1/2)
You can use capturing groups and backreferences to do it.
Check out this page, under "Replacement Text Syntax" for more details.
Here's a fiddle that does what you ask for.
Hope this helps.

Javascript regex to check if a value begins with a number followed by anything else

How would i go about doing a regex to see if it begins with a number and any character can follow after. My current expression is
var validaddress = /^[0-9][A-Za-z0-9]+$/;
But this isn't the right way. Im new to this, help anyone?
If you need character(s) after the digit, try this:
var validaddress = /^[0-9].+$/;
If characters after the digit are optional, use this:
var validaddress = /^[0-9].*$/;
What you looking for is: var validaddress = /^\d.*$/;
\d - Matches any digit
.* - Matches any character except newline zero or more times.
Or replace .* with .+, if you are looking for at least 1 character.
Try /^[0-9]/ as the regular expression.
If it only needs to start with a number, I'd only check that...
when you say "any character follow" -- do you mean any alphanumeric character or just anything (i.e. including space, comma, slash etc)?
if it is the latter, how about this:
var validaddress = /^[0-9].+$/;
I suppose this should work
/^\d+.+$/
I would get rid of the $. Also, a '.' would suffice for "any character". This one works for me:
var validaddress = ^[0-9].+;
If you have a string of these values and you want to find each individually try this:
(^|(?<=\W))(\d\w*)
You can then do a loop through each match.
Regexr example
You could also use /^\d/ as the briefest approach.

Categories