Regex - match variable beginned with $ in javascript? - javascript

I want to match all the variables like $bar, $foo, I only have this until the moment:
(\$)+[A-Za-z]

Just this:
/\$[\w_]+/g
And if you want to check the variable does not start with number, just check by negative lookahead. (?![\d])
/\$(?![\d])[\w_]+/g
/g to match all.

This should match some common valid variables which consists of [A-Za-z0-9_] but don't start with number.
/(\$(?!\d)\w+)/g
/ .. /g // regular expression is put between two //, the g behind is flag for global
( .. ) // a capturing group, can be called using \1 or $1 depending on regex processor
\$ // escaped character `$`
(?!\d) // negative lookahead - ensures that next character after $ isn't a match ->
-> \d // matches one digit
\w+ // one or more "word characters". Matches the ASCII characters [A-Za-z0-9_]
note: if you are going to match all variable names globally, you don't need any capturing groups, so just use /\$(?!\d)\w+/g.
According to php.net, this is the right regexp: \$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
EDIT: I have just noticed you are not matching PHP variables at all, but you get the idea now. :)
For fun http://regex.alf.nu/

\$[A-Za-z]+\w*
Explained: $ in the start, one or more letters, followed by 0 or more valid variable letters (depending on language).
See https://www.regex101.com/r/cB1bM1/1 for a detailed explanation.

The regex you may need is:
\$\w+

Related

Regex for a string which do not start with a number and allow only alphanumeric

I am trying to create a javascript regex for below conditions
Allow Alphanumeric only
But also allow underscore(_)
Don't allow to start with a number
Don't allow to start with an underscore
I have created a regex ^(?![0-9]|[_].*$).* which will work for last two conditions above. Please suggest how can I add an and condition to make it work for all above scenarios.
You may use the following regex:
^[A-Za-z]\w*$
Details
^ - start of string
[A-Za-z] - any ASCII letter
\w* - zero or more letters/digits/_
$ - end of string.
To allow an empty string match, wrap the whole pattern with an optional non-capturing group:
^(?:[A-Za-z]\w*)?$
^^^ ^^
You can use this regex:
^(?![0-9_])\w+$
RegEx Demo
(?![0-9_]) is negative lookahead to fail the match when we have a digit or _ at the start.
you can use the regex
^[a-zA-Z][A-Za-z0-9_]*$
see the regex101 demo
You may be thinking too literally about the last two requirements. If it's alphanumeric (so.. a-z and 0-9, right?) then saying "dont allow numbers or underscore at the start" is probably the same as "must start with a letter"
^[a-z][a-z0-9_]*$
This is "must start with a-z", followed by "must follow with zero or more letters, numbers or underscores. The ^ outside of a character class (for example [a-z] is a character class) means "start of input". The $ means end of input.
If you interpreted the last two requirements literally, you could write:
[^0-9_]
This means "any character that is not 0-9 and also not an underscore" but it doesn't necessarily restrict the user from entering something other than a-z as the first character, so they might enter a #, and it would pass..

JS Regex lookahead

I want to be able to match these types of strings (comma separated, and no beginning or trailing spaces):
LaunchTool[0].Label,LaunchTool[0].URI,LaunchTool[1].Label,LaunchTool[1].URI,LaunchItg[0].Label,LaunchItg[0].URI,csr_description
The rules, in English, are:
1) Zero or more instances of [] where the brackets must contain only one number 0-9
2) Zero or more instances of ., where . must be followed by a letter
3) Zero or more instances of _, where _ must be followed by a letter
I currently have this regex:
/^([a-z]){1,}(\[[0-9]\]){0,}(\.){0,}[a-z]{1,}$/i
I cannot figure out why
"aaaa" doesn't match
furthemore,
"aaaa[0].a" matches, but "aaaa[0]" does not...
anyone know what's wrong? I believe I might need a lookahead to make sure . and _ characters are followed by a letter? Perhaps I can avoid it.
this regex can match "aaaa", try getting value of
(/^([a-z]){1,}(\[[0-9]\]){0,}(\.){0,}[a-z]{1,}$/i).test("aaaa")
"aaaa[0]" does not match, because there is [a-z]{1,} in the end of expression. once "[0]" is matched by (\[[0-9]\]){0,}, trailing [a-z]{1,} must be shown at the end of string
Use optional capture groups. Example: ([a-z])?.
Here is what i ended up with:
/^((\w+)(\[\d+\])?\.?(\w+)?,?)+$/
Shorthands explanation:
* = {0,}
+ = {1,}
\w = [A-Za-z0-9_]
\d = [0-9]

How to extract the last word in a string with a JavaScript regex?

I need is the last match. In the case below the word test without the $ signs or any other special character:
Test String:
$this$ $is$ $a$ $test$
Regex:
\b(\w+)\b
The $ represents the end of the string, so...
\b(\w+)$
However, your test string seems to have dollar sign delimiters, so if those are always there, then you can use that instead of \b.
\$(\w+)\$$
var s = "$this$ $is$ $a$ $test$";
document.body.textContent = /\$(\w+)\$$/.exec(s)[1];
If there could be trailing spaces, then add \s* before the end.
\$(\w+)\$\s*$
And finally, if there could be other non-word stuff at the end, then use \W* instead.
\b(\w+)\W*$
In some cases a word may be proceeded by non-word characters, for example, take the following sentence:
Marvelous Marvin Hagler was a very talented boxer!
If we want to match the word boxer all previous answers will not suffice due the fact we have an exclamation mark character proceeding the word. In order for us to ensure a successful capture the following expression will suffice and in addition take into account extraneous whitespace, newlines and any non-word character.
[a-zA-Z]+?(?=\s*?[^\w]*?$)
https://regex101.com/r/D3bRHW/1
We are informing upon the following:
We are looking for letters only, either uppercase or lowercase.
We will expand only as necessary.
We leverage a positive lookahead.
We exclude any word boundary.
We expand that exclusion,
We assert end of line.
The benefit here are that we do not need to assert any flags or word boundaries, it will take into account non-word characters and we do not need to reach for negate.
var input = "$this$ $is$ $a$ $test$";
If you use var result = input.match("\b(\w+)\b") an array of all the matches will be returned next you can get it by using pop() on the result or by doing: result[result.length]
Your regex will find a word, and since regexes operate left to right it will find the first word.
A \w+ matches as many consecutive alphanumeric character as it can, but it must match at least 1.
A \b matches an alphanumeric character next to a non-alphanumeric character. In your case this matches the '$' characters.
What you need is to anchor your regex to the end of the input which is denoted in a regex by the $ character.
To support an input that may have more than just a '$' character at the end of the line, spaces or a period for instance, you can use \W+ which matches as many non-alphanumeric characters as it can:
\$(\w+)\W+$
Avoid regex - use .split and .pop the result. Use .replace to remove the special characters:
var match = str.split(' ').pop().replace(/[^\w\s]/gi, '');
DEMO

Query on Javascript RegEx

I need a regex that allows 0-9, a-z, A-Z, hyphen, question mark and "/" slash characters alone. Also the length should be between 5 to 15 only.
I tried as follows, but it does not work:
var reg3 = /^([a-zA-Z0-9?-]){4,15}+$/;
alert(reg3.test("abcd-"));
length should be between 5 to 15 only
Is that why you have this?
{4,15}+
Just use {5,15}; it’s already a quantifier, and a + after it won’t work. Apart from that, the group isn’t necessary, but things should work.
/^[a-zA-Z0-9?/-]{5,15}$/
(I also added a slash character.)
This is what you need:
if (/^([a-z\/?-]{4,15})$/i.test(subject)) {
// Successful match
} else {
// Match attempt failed
}
REGEX EXPLANATION
^([a-z\/?-]{4,15})$
Options: Case insensitive
Assert position at the beginning of the string «^»
Match the regex below and capture its match into backreference number 1 «([a-z\/?-]{4,15})»
Match a single character present in the list below «[a-z\/?-]{4,15}»
Between 4 and 15 times, as many times as possible, giving back as needed (greedy) «{4,15}»
A character in the range between “a” and “z” (case insensitive) «a-z»
The literal character “/” «\/»
The literal character “?” «?»
The literal character “-” «-»
Assert position at the very end of the string «$»
Couple issues,
you need {5,15} instead of {4,15}+
need to include /
Your code can be rewritten as
var reg3 = new RegExp('^[a-z0-9?/-]{5,15}$', 'i'); // i flag to eliminate need of A-Z
alert(reg3.test("a1?-A7="));
Update
Let's not confuse can be with MUST be and concentrate on the actual thing I was trying to convey.
{4,15}+ part in /^([a-zA-Z0-9?-]){4,15}+$/ should be written as {5,15}, and / must be included; which will make your regexp
/^([a-zA-Z0-9?/-]){5,15}$/
which CAN be written as
/^[a-z0-9?/-]{5,15}$/i // i flag to eliminate need of A-Z
Also I hope everybody is OK with use of /i

Regex to match card code input

How can I write a regex to match strings following these rules?
1 letter followed by 4 letters or numbers, then
5 letters or numbers, then
3 letters or numbers followed by a number and one of the following signs: ! & # ?
I need to allow input as a 15-character string or as 3 groups of 5 chars separated by one space.
I'm implementing this in JavaScript.
I'm not going to write out the whole regex for you since this is homework, but here are some hints which should help you out:
Use character classes. [A-Z] matches all uppercase. [a-z] matches all lowercase. [0-9] matches numbers. You can combine them like so [A-Za-z0-9].
Use quantifiers like {n} so [A-Z]{3} gives you 3 uppercase letters.
You can put other characters in character classes. Let's say you wanted to match % or # or #, you could do [%##] which would match any of those characters.
Some meta-characters (characters which have special meaning in the context of regular expressions) will need to be escaped like so: \$ (since $ matches the end of a line)
^ and $ match the beginning and end of the line respectively.
\s matches white-space, but if you sanitize your input, you shouldn't need to use this.
Flags after the regex do special things. For example in /[a-z]/i, the i ignores case.
This should be it:
/^[a-z][a-z0-9]{4} ?[a-z0-9]{5} ?[a-z0-9]{3}[0-9][!&#?]$/i
Feel free to change 0-9 and [0-9] with \d if you see fit.
The regex is simple and readable enough. ^ and $ make sure this is a whole match, so there aren't extra characters before or after the code, and the /i flag allows upper or lower case letters.
I would start with a tutorial.
Pay attention to the quantifiers (like {N}) and character classes (like [a-zA-Z])
^[a-zA-Z][a-zA-Z0-9]{4} ?[a-zA-Z0-9]{5} ?[a-zA-Z0-9]{3}[\!\&\#\?]$

Categories

Resources