Regex first letter not integer with jquery [closed] - javascript

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 6 years ago.
Improve this question
I have textbox and I want get string value. But I want users to not be able to enter the string that has number on first letter. As matter of fact I want to replace number with '' null.
for example
1test =====convert=======> test

you can simply use ^[a-zA-Z]
^ starts with a-z or A-Z
or if you want special character too then use ^\D
^\D : Matches anything other than a decimal digit
Regex Demo
you can use $text.replace(/^[^0-9]+/, '')
/^ beginning of the line
[^0-9]+ match anything other than digits at-least once
thanks # Wiktor and Tushar

here is the solution: You can check on this live regex.
https://regex101.com/r/OJfyv4/1
$re = '/\b[a-z][a-z0-9]*/';
$str = '1test';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);

This works your case:
^\d+
https://regex101.com/r/daezA9/1
^ asserts position at start of the string
\d matches a digit (equal to [0-9])

Related

How to remove digits from string. based on the length and remove other letters with it [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 2 years ago.
Improve this question
const item = "Dell Model 23506A Laptop M2";
const item2 = item.replace(/\d{2,}/g, "");
The above code will remove any words with "more than 2 digits found in a row, but will only remove the numbers from the Word.
Example the end result of the above will be "Dell Model A Laptop M2", leaving the A from 23506A.
How do you write the logic, that if more than 2 digits found in a row in a Word, to remove the entire word as a result.
Example the end result of the above should be "Dell Model Laptop M2"
Thus removing 23056A entirely, because more than 2 digits we're found in a row (right after another).
This should work:
item.replace(/[^\s]*\d{2,}[^\s]*/g, "");
You may also wish to get rid of the adjacent space:
item.replace(/[^\s]*\d{2,}[^\s]*\s?/g, "");
If you don't want to remove all other strings that could possibly contain 2 digits like for example 1,50, you can assert at least a char a-zA-Z
\b(?=\w*[A-Za-z])\w*\d{2}\w*\b
Explanation
\b A word boundary
(?=\w*[A-Za-z]) Positive lookahead, assert a char a-zA-Z
\w*\d{2}\w* Match 2 digits between optional word characters
\b A word boundary
Regex demo

How user regex to have one or more uppercase letter separated by comma? [closed]

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

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.

Javascript regex match (random string in the middle) [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 want to test if a string can match a pattern in javascript
problem is there is a random string in the middle
pattern: "account/os/some_random_string/call_back"
so a string look like below will match
var mystring = "account/os/1234567/call_back"
thanks
You want a regex for starts with account/os/ and ends with /call_back, here's one:
/^account\/os\/.*\/call_back$/
.* will match any random string (including the empty string.). If you want a minimum length on the random string you change the *:
.* : 0 or more characters
.+ : 1 or more characters
.{n,} : n or more characters (replace n with an actual number)
Well, it depends. If you want every single character between account/os/ and /call_back, use this:
var randomStr = mystring.match(/account\/os\/(.*)\/call_back/)[1];
The match will return an array with 2 elements, the first one with the entire match, the 2nd one with the group (.*). If you are completely sure that you have at least one character there, replace * with +.
If you know something more specific about the text you have to collect, here are some replacements for the .(dot is matching almost everything):
[A-z] for any of A, B, .. , Y, Z, a, b, .. , y, z
[0-9] for any digit
You can mix then and go fancy, like this:
[A-Ea-e0-36-8]
So, your pattern may look like this one:
/account\/os\/([A-Ea-e0-36-8]*)\/call_back/
Your example have a number there, so you are probably looking for:
/account\/os\/([0-9]*)\/call_back/
or
/account\/os\/(\d*)\/call_back/
.. it's the same thing.
Hope that helps.
Edit: What JS answer doesn't have a jsfiddle? http://jsfiddle.net/U2Jhw/

I need Regular Expression for a string [closed]

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 8 years ago.
Improve this question
I need a regular expression for a string that
starts with Alphabets (no number)
max Length 8
No special characters or space.
string can have number or _ except for starting character.
This would work:
/^[a-z][a-z0-9_]{0,7}$/i
For example,
/^[a-z][a-z0-9_]{0,7}$/i.test('a1234567'); // true
/^[a-z][a-z0-9_]{0,7}$/i.test('01234567'); // false
The \w shorthand is for all letters, numbers and underscores. [A-Za-z] is overkill, the /i flag will get you all letters, case insensitive.
Therefore, a super simple regex for what you need is:
/^[a-z]\w{0,7}$/i
/^[a-z]\w{0,7}$/i.test("a1234567");
> true
/^[a-z]\w{0,7}$/i.test("a12345697");
> false
/^[a-z]\w{0,7}$/i.test("01234567");
> false
Try this out:
/^[A-Za-z]{1}[a-zA-Z0-9_]{0,7}$/
Try this one:
/^[a-zA-Z][0-9a-zA-Z_]{0,7}$/
This requires an alpha start character, and optionally allows up to 7 more characters which are either alphanumeric or underscore.
EDIT: Thanks, Jesse for the correction.
And another version with lookaheads :)
if (subject.match(/^(?=[a-z]\w{0,7}$)/i)) {
// Successful match
}
Explanation :
"^" + // Assert position at the beginning of the string
"(?=" + // Assert that the regex below can be matched, starting at this position (positive lookahead)
"[a-z]" + // Match a single character in the range between “a” and “z”
"\\w" + // Match a single character that is a “word character” (letters, digits, etc.)
"{0,7}" + // Between zero and 7 times, as many times as possible, giving back as needed (greedy)
"$" + // Assert position at the end of the string (or before the line break at the end of the string, if any)
")"

Categories