Regular Expression (Regex) on CSS3 Transform String [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 7 years ago.
Improve this question
Given a string "translateX(-50%) scale(1.2)" with N transform functions
1) How can I match the names ["translateX", "scale"]?
2) How can I match the values ["-50%", "1.2"]?

If you absolutely must use regular expression to do this, you can use the exec() method in a loop, pushing the match result of the captured group(s) to the desired arrays of choice.
var str = 'translateX(-50%) scale(1.2)'
var re = /(\w+)\(([^)]*)\)/g,
names = [], vals = [];
while (m = re.exec(str)) {
names.push(m[1]), vals.push(m[2]);
}
console.log(names) //=> [ 'translateX', 'scale' ]
console.log(vals) //=> [ '-50%', '1.2' ]
The regular expression uses two capture groups, the first matches/captures word characters only, the second uses negation which will match any character except ) "zero or more" times.

Try something like (\w+)\((.+?)\):
(\w+): Match the regular expression below and capture its match into backreference number 1
\w+: Match a single character that is a “word character” (letters, digits, and underscores)
+: Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\(: Match the character “(” literally
(.+?): Match the regular expression below and capture its match into backreference number 2
.+?: Match any single character that is not a line break character
+?: Between one and unlimited times, as few times as possible, expanding as needed (lazy)
\): Match the character “)” literally
var str = "translateX(-50%) scale(1.2)",
regex = /(\w+)\((.+?)\)/g,
match, names = [], values = [];
while(match = regex.exec(str)) {
names.push(match[1]);
values.push(match[2]);
}

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.

Regex to find digits group after a text match [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 4 years ago.
Improve this question
I have tried many regex expressions but unable to get the desired output.
My string is
Ms.Evalyn J Hobbs AND Mr.Jan K Hir sch AND Ms.Gale D Bannister 3611
I need to extract the digits group which come after Mr or Mrs that is 3611
Try Regex: (?:Ms|Mr|Mrs).*?(\d+) and get Group 1 value
Demo
Another way to capture digits in a group after Ms., Mr. Ms. (or maybe Miss.) could be:
\bM(?:rs?|s|iss)\.[^\d]+(\d+)
That would match
A word boundary \b to make sure it is not part of a larger match and then M
An alternation (?:rs?|s|iss)\. that would match one of the variants followed by a dot.
Match not a digit using a negated character class [^\d]+
At the end capture one or more digits in a capturing group (\d+)
const regex = /\bM(?:rs?|s|iss)\.[^\d]+(\d+)/g;
const str = "Ms.Evalyn 33 J Hobbs AND Mr.Jan K Hir 55 years Mr. sch AND Ms.Gale D Bannister 3611";
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log(m[1]);
}

Constructing a regex with three subpatterns and a maximum length of 28 characters [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 4 years ago.
Improve this question
I need to create an expression that fit this requirements :
The string must be composed by 3 substring
The first substring accept 0-9a-zA-Z, the minimum length is 1 and there is notte max length
The second substring must be " - "
The last have The first's one same condition
Total maximum string length must be 28 chars
It is possible to accomplish this requirement with Regex?
Following regex should work fine:
/(?=^.{3,28}$)[^\W_]+\-[^\W_]+/
var array = [
"123456790-123456789012345678",
"123456790-1234567890123456789",
"adsd-dsds"
];
var re = /(?=^.{3,28}$)[^\W_]+\-[^\W_]+/;
array.forEach(e => console.log(re.test(e) ? e.match(re)[0]: "match failed"));
Breakdown shamelessly copied from regex101.com:
Positive Lookahead (?=^.{3,28}$)
^ asserts position at start of a line
.{3,28} matches any character (except for line terminators)
{3,28} Quantifier — Matches between 3 and 28 times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line
Match a single character not present in the list below [^\W_]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\W matches any non-word character (equal to [^a-zA-Z0-9_])
_ matches the character _ literally (case sensitive)
\- matches the character - literally (case sensitive)
Match a single character not present in the list below [^\W_]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\W matches any non-word character (equal to [^a-zA-Z0-9_])
_ matches the character _ literally (case sensitive)

(\d+,\d+) add so it is imuneto white spaces [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
How in javascript to make regex to recognize and extract integer numbers for coordinates which have format like
( number1, number2 )
between ( and number1 and , and number2 and ) can be arbitrary number of whitespaces (user are going to enter coordinates so I don't want to force strict format without whitespaces)
(\d+,\d+)
what to add to this so it works ?
There are a few choices, pending your actual input.
To match all "whitespace characters" (space, tab, carriage return, newline and form feed), you can use the \s shorthand approach. If you want a number, in this case \d+, to be surrounded by "0 or more" of these, you would use:
\s*\d+\s*
In your full pattern:
\( # opening parentheses
\s*\d+\s*, # first number followed by a comma
\s*\d+\s* # second number
\) # closing parentheses
Note: The parentheses are escaped here as they're special characters in a regular expression pattern.
Now, if you don't want to match "all whitespace" and were only interested in plain spaces, for example, you could use a matching character set of [ ] (i.e. a space between two brackets). In the pattern from above:
\(
[ ]*\d+[ ]*,
[ ]*\d+[ ]*
\)
Not really sure how you want to use the matches, I'm assuming you want the numbers returned individually so in that case, you can use the following:
var str = '(1, 2)';
var matches = str.match(/\(\s*(\d+)\s*,\s*(\d+)\s*\)/);
if (matches) {
var firstNumber = matches[1];
var secondNumber = matches[2];
// do stuffs
}
Note: In the pattern I used here, I've wrapped the \d+s in parentheses; this will "capture" those values in to groups which are then accessible by their "group index". So, the first (\d+) will be available in matches[1] and the second will be available in matches[2].
Try this regex: \(\s*\d+\s*,\s*\d+\s*\).
Fiddle

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