JavaScript RegEx match all characters from set except some [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 9 years ago.
Improve this question
I need to test whether a string contains only punctuation symbols except one character symbol say hypen (-)
'??...' -> true
'.!sf' -> false
'..-??' -> false
I use library XRegEx library that allows to match for example punctuation with p{P} but I need to exclude some characters from this match.
I use following pattern:
new XRegExp("^\\p{P}+$")
How can I except hypen symbol from "-" this match?
N.B. Original question was about "leters":
I need to test whether a string contains letters except one character say letter "m"

/^[a-ln-z]+$/.test('abcfx')
// true
/^[a-ln-z]+$/.test('abcfx12.!')
// false
/^[a-ln-z]+$/.test('abcmfx')
// false
Using positive lookahead:
/^(?=[^m]+$)[a-z]+$/.test('abcfx')
// true
/^(?=[^m]+$)[a-z]+$/.test('abcfx12.!')
// false
/^(?=[^m]+$)[a-z]+$/.test('abcmfx')
// false

What you are looking for is probably this regex, which accepts ranges from a to l and from n to z
^[a-ln-z]+$

Related

javascript regex get specific substring [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 6 months ago.
Improve this question
How could I get the specific substring between 'Y___(string i want to get)___N'?
For example:
"Y___INT_GET_ERROR_CONFIGS___N"
"INT_GET_ERROR_CONFIGS"
You can get everything inside by Y___(.*?)___N, you can use matchAll to get all instance that matches this case, and you can loop through and get the group value.
const str = `Y___INT_GET_ERROR_CONFIGS___N
INT_GET_ERROR_CONFIGS Y___INT_GET_ERROR_SOMETHING___N`
const result = str.matchAll(/Y___(.*?)___N/g);
for (match of result) {
console.log(match[1])
}
If it's just the first occurrence you wish you match, then:
'Y___(string you want to get)___N'.match(/(?<=Y___).+(?=___N)/)
Result:
"(string you want to get)"
If you want all such occurrences to be returned, then use the g flag:
`Y___(string you want to get)___N
.
.
.
Y___(second string you want)___N`.match(/(?<=Y___).+(?=___N)/g)
Result:
["(string you want to get)", "(second string you want)"]
Explanation:
(?<=Y___): A positive lookbehind stipulates that matches will be preceded by the contents of the lookbehind, namely "Y___". The contents of the lookbehind does not form part of the match result, and also does not consume characters during matching.
.+: Matches at least one instance of any character, but will match as many as possible.
(?=___N): A positive lookahead stipulates that matches will be proceeded by the contents of the lookahead, namely "___N". The contents of the lookahead does not form part of the match result, nor does it consume characters during matching.

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

Javascript Regex to match $, but not ${ [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 6 years ago.
Improve this question
I have just created a snippet generator tool for Sublime Text, Atom and VS Code, you can find it here: https://snippets.now.sh.
Snippets for these apps need to have the $ escaped e.g. $('.class'), but not when it is used for placeholders e.g. ${1:foo}.
What is the regex to match only occurrences of the $ when it is not followed by a {?
Just to reiterate:
Match this: $foo
Don't match this: ${foo
Use the following regex pattern:
\$(?!{).+\b
(?!{) - negative lookahead assertion, ensures that $ is not followed by {
https://regex101.com/r/qQZIZQ/2
Additional case with substitution for the condition :
$ dollar signs need to be escaped, like so \$, but not when followed
by {, like so; ${
\$(?!{)(.+\b)?
substitution: \\$0
https://regex101.com/r/qQZIZQ/4

Regex first letter not integer with jquery [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 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])

Is it possible to check if there are multiple special characters in input using regular expression in 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 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

Categories