How to build this a regex match in javascript - javascript

I have this string
[X=(any number)] (any character, include space) [/X]
Example: [X=5]Test string[/X]
I test with /(\[)(X=)(\d+\])(\w\s\.)(\[/X\])/gi but it doesn't work.

The problem is that the / needs to be escaped as well. Also it's not very useful to group constant matches. Lastly, you're matching (\w\s.) but this only allows one word character, one space character and one character of any type, in this order. I don't see what's the use of that. Try:
/\[X=(\d+)\](.+)\[\/X\]/gi
This will group the two variable parts.

Use this regex:
/\[X=\d+\].+\[\/X\]/gi
It checks if the given string is in the desired format.

var myregexp = /\[X=(\d+)\]([^[]*)\[\/X\]/g;
works for me, including if test string is multi-lines. Tested with RegexBuddy

Related

Javascript RegEx Templating Edge Case

I have a RegEx implemented with JavaScript that is close to doing what I want. However, I am having an issue figuring out the last piece which is causing an issue with an edge case. Here is the RegEx that I have so far:
/\$\{(.+?(}\(.+?\)|}))/g
The idea is that this RegEx would use a templating system to replace/inject variables in a string based on templated variables. Here is an example of the edge case issue:
"Here is a template string ${G:SomeVar:G${G:SomeVar:G} that value gets injected in."
The problem is the RegEx is matching this:
"${G:SomeVar:G${G:SomeVar:G}"
What I want it to match is this:
"${G:SomeVar:G}"
How would I get the RegEx to match the expected variable in this edge case?
You have an alternation in your pattern to either stop at } or also match a following (...) after it.
As the dot can match any character, you can use a negated character class to exclude matching { } ( )
If you want to match ${G:SomeVar:G} but also ${G:SomeVar:G}(test) you can add an optional non capture group after it.
For a match only, you can omit the capture groups.
\$\{[^{}]*}(?:\([^()]*\))?
See a regex101 demo.
If the format of the string with the : and the same character before and after it should be matched, you can use a capture group with a backreference:
\$\{([A-Z]):[^{}]*?:\1}(?:\([^()]*\))?
See a regex101 demo.
Instead of matching anything with (.+?), change it to not match another closing brace or dollar sign, [^{$].
\$\{([^{$]+?(}\(.+?\)|}))

Regex testing for special characters

I'm trying to write a regex to test for certain special characters, but I think I am overcomplicating things. The characters I need to check for are: &<>'"
My current regex looks like such:
/&<>'"/
Another I was trying is:
/\&\<\>\'\"/
Any tips for a beginner (in regards to regex)? Thanks!
You are looking for a character class:
/[&<>'"]/
In doing so, any of the characters in the square brackets will be matched.
The expression you were originally using, /&<>'"/, wasn't working as expected because it matches the characters in that sequential order. In other words, it would match a full string such as &<>'" but not &<.
I'm assuming that you want to be able to match all of the characters you listed, at one time.
If so, you should be able to combine a character set with the g (global-matching) flag, for your regex.
Here's what it could look like:
/[<>&'"]/g
Try /(\&|\<|>|\'|\")/
it depends on what regex system you use

Find character pattern using regex

I am trying to find all occurrences of a special character / surrounded by either letters or numbers.
After many tries, I have come up with the following Regex that almost does what I need:
(?![a-z0-9])\/(?=[a-z0-9])
This works fine for these examples:
aa/aa
123/123
aa/123
However, it fails if there are two forward slashes together:
http://regexr.com/
In this case, it matches the second forward slash after http which I do not want.
How can I modify this Regex to meet my needs?
EDIT: I do not want to a match when two forward slashes are together. I only want to match if a single forward slash is surrounded by alphanumeric characters.
you would need a positive lookbehind group, like so:
(?<=[a-z0-9])+\/{1}(?=[a-z0-9]+)
however, according to http://regexr.com/ it is not supported in javascript.
Works fine in e.g. python http://pythex.org/
Easy!
(?![a-z0-9])\/+(?=[a-z0-9])
You should have put + for 1 on more occurrence of a character. So you should have written \/+ instead of just \/.
Try this
(!?[a-z0-9])\/(?=[a-z0-9])
Try this
[a-z0-9](\/)[a-z0-9]
Regex demo
Explanation:
( … ): Capturing group sample
\: Escapes a special character sample

Combining 2 regexes, one with exact match using OR operator

I am trying to combine:
^[a-zA-Z.][a-zA-Z'\\- .]*$
with
(\W|^)first\sname(\W|$)
which should check for the exact phrase, first name, if that is correct. It should match either the first regex OR the second exact match. I tried this, but appears invalid:
^(([a-zA-Z.][a-zA-Z'\\- .]*$)|((\W|^)first\sname(\W|$))
This is in javascript btw.
Combining regular expressions generally can be done simply in the following way:
Regex1 + Regex2 = (Regex1|Regex2)
^[a-zA-Z.][a-zA-Z'\\- .]*$
+ (\W|^)first\sname(\W|$) =
(^[a-zA-Z.][a-zA-Z'\\- .]*$|(\W|^)first\sname(\W|$))
Because some SO users have a hard time understand the math analogy, here's a full word explanation.
If you have a regex with content REGEX1 and a second regex with content REGEX2 and you want to combine them in the way that was described by OP in his question, a simple way to do this without optimization is the following.
(REGEX1|REGEX2)
Where you surround both regular expressions with parenthesis and divide the two with |.
Your regex would be the following:
(^[a-zA-Z.][a-zA-Z'\\- .]*$|(\W|^)first\sname(\W|$))
Your first regex has an error in it, though, that makes it invalid. Try this instead.
(^[a-zA-Z.][a-zA-Z'\- .]*$|(\W|^)first\sname(\W|$))
You had \\ in the second character class where you wanted \
The problem is that the first regex is messed up. You don't need to double escape characters. Therefore
\\-
Will match an ascii character between \(92) and (32). Remove one of the slashes.
Reference

Javascript Regex Object Doesn't Recognize {n,m}

I'm trying to write a regular expression in JS to recognize any digit up to seven times, followed by a "-" followed by 2 digits followed by "-" followed by a single digit. This is the simple regex I have:
/\d{1,7}-\d{2}-\d/g
This should match strings like:
123-12-7
1-12-7
1234567-12-7
but not 12345678-12-1
However, the above is returning true. The regex returns true when there is any number of digit in the first group.
Does the JavaScript Regex object not support {n,m}?
Here is an example of what I am talking about.
var pattern = new RegExp(/\d{1,7}-\d{2}-\d/);
alert(pattern.test("12345678-13-1"));
http://jsfiddle.net/XTRAc/1/ live example
It matches 2345678-13-1. You need to anchor it to the beginning and end of your string:
/^\d{1,7}-\d{2}-\d$/
Note though, that (as Rocket Hazmat pointed out) you do not need to use the RegExp constructor if you use a regex literal (something without string quotes).
JSFiddle
It does support the {min,max}-syntax, but .match and .test() try to find matching substrings. You will have to include start and end anchors. Also notice that you should either use the RegExp constructor to build a regex from a string or a regex literal, but not both (see MDN: creating regexes).
/^\d{1,7}-\d{2}-\d$/
new RegExp("^\\d{1,7}-\\d{2}-\\d$") // the worse choice
You are constructing your regex incorrectly. Try this (note the anchors, which ensure the string consists of nothing but your pattern):
var pattern= /^\d{1,7}-\d{2}-\d$/;
Otherwise subsets of the existing string will match your regex.
If you need to validate entire input string, use regex pattern
/^\d{1,7}-\d{2}-\d$/
If you need to validate entire line of input string, use regex pattern
/^\d{1,7}-\d{2}-\d$/mg
If you need to find matches within input string, use regex pattern
/(?:\D|^)(\d{1,7}-\d{2}-\d)(?!\d)/g
...and use $1 as a result.
It does support the {n,m} part, the problem here is that your example matches 2345678, so you would need a way of matching the character before the first set of digits

Categories

Resources