Javascript Regex to match $, but not ${ [closed] - javascript

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

Related

Regex to Match (number-number) in Javascript [closed]

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 7 years ago.
Improve this question
How do I detect strings in Javascript that end with (number-number) at end of string?
Examples of strings I'm looking to match:
High (0-22)
Low (23-100)
Regex
/.*\(\d+-\d+\)$/
Explanation
. = any character
* = zero or more times
\(,\) = literal parentheses (must escape with backslash or it will be interpreted as a regex control character)
\d = digit
+ = one or more times
- = literal hyphen
$ = end of line
Links
Here is a jsfiddle to play with.
http://www.regular-expressions.info/ has lots of useful information.

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

JavaScript RegEx match all characters from set except some [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 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]+$

What is JavaScript regex for string matches which starts with $$ and ends with $$? [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 want Javascript regex for all strings that start with $$ and ends with $$
example:
$$hello$$
$$world$$
^\${2}.*?\${2}$
How this works: first, you look for two dollar signs, then, it allows any input, and then it looks whether the string ends with two dollar signs.
A pattern like this should work:
^\$\$.*\$\$$
This will match the beginning of the string (^) followed by $$, followed by zero or more of any character, followed by $$, and the end of the string ($). For example:
/^\$\$.*\$\$$/.test('$$hello$$') // true
If you're looking for a substring of a larger string which matches this pattern, use something like this:
\$\$.*?\$\$
This will match $$, followed by zero or more of any character, non-greedily, followed by $$. For example:
/\$\$.*?\$\$/.exec('print "$$hello$$"') // ['$$hello$$']
But for something this simple, you can just use plain old string manipulation:
var string = '$$hello$$';
var result = (string.substr(0, 2) == '$$') && (string.substr(-2) == '$$'); // true

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.

Categories