Find character pattern using regex - javascript

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

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, [^{$].
\$\{([^{$]+?(}\(.+?\)|}))

JavaScript regex to capture everything after the second to last backslash

I have a regex that looks like the following currently:
/^.*[\\\/]/
This will strip every single backslash from a string. The problem I'm facing is I have to now be able to capture everything from the second to last backslash.
Example:
/Users/foo/a/b/c would return b/c
/Another/example/ would return Another/Example
So I need to capture everything after the second to last backslash. How would the regex above do that?
Try with this simple solution:
s = "aaaa/bbbb/cccc/dddd";
s.split("/").slice(-2).join("/"); /* It will return "cccc/dddd" */
I assume that you mean forward slash, not backslash.
Here is a regex alternative to pierlauro's answer.
/([^\/]+\/[^\/]+)\/?$/
Regex101
As pierlauro's answer shows, split, join, and slice are probably the best options for this. But if you MUST use a regex (not sure why), you could employ something like the following:
\/?(\[^\/\]+?\/\[^\/\]+)\/?$
This regex accommodates for optional trailing slashes and for urls shorter than 2 /s. It leverages the $ character to focus our search scope on the end of the string.

Regex which returns false when string contains 2 non consecutive forward slashes (negative lookahead)

I'm trying to build a regex which should match when only one forward slash is found and false when 2 or more forward slashes are found. The capturing group is not used, olny if it matches, and the regex is executed by javascript.
/this-should-match
/this-should/not-match
I've tried a couple of regexps, including using a negative lookahead, but I can't seem to find the solution. Some patterns I've tried:
/\/(.*)(?!\/)/i
/\/(.*)[?!\/]/i
/\/(.*[?!\/])/i
Any regex genius over here knows the solution? I'm aware regex is meant to find an occurrence of a pattern, but there should be some solution for this?
Use negated character class instead of look arounds.
^\/[^/]+$
^ Anchors the regex at the start of the string.
[^/] negated character class. Matches anything other than /
$ Anchors the regex at the end of the string. Ensures that nothing follows the string that is matched by the pattern.
Regex Demo
Example
"/this-should-match".match(/^\/[^/]+$/)
=> ["/this-should-match"]
"/this-should-match/not-match".match(/^\/[^/]+$/)
=> null

Regex matches in regexpal but not in javascript?

I am trying to find a regex that will work for validating URLs. I found this guy:
^(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,#?^=%&:/~+#-]*[\w#?^=%&/~+#-])?$
Which worked pretty well when I tested it using regexpal, but when I actually plug it into my javascript it fails to match. Fiddle here.
I am testing against this URL:
http://s3.amazonaws.com/SomeShow/Podcasts/HouroneofWhatever234.mp3
Can anyone see why it would match in regexpal, but not when I try to use it in my javascript?
Use a regex literal:
/^(http|ftp|https):\/\/[\w-]+...$/
(you also have to escape the slashes to prevent the them to be interpreted as ending regex terminal symbol)
If you use a string, you have to escape every backslash, because the backslash is the escape characters in strings as well.
new RegExp("^(http|ftp|https)://[\\w-]+...$")
In your current expression, "[\w-]+" will turn to [w]+ because \w is not a valid escape sequence in strings.
See: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions#Creating_a_Regular_Expression

Regex for number surrounded by slashes

Like the title says, I have a (faulty) Regex in JavaScript, that should check for a "2" character (in this case) surrounded by slashes. So if the URL was http://localhost/page/2/ the Regex would pass.
In my case I have something like http://localhost/?page=2 and the Regex still passes.
I'm not sure why. Could anyone tell me what's wrong with it?
/^(.*?)\b2\b(.*?$)/
(I'm going to tell you, I didn't write this code and I have no idea how it works, cause I'm really bad with Regex)
Seems too simple but shouldn't this work?:
/\/2\//
http://jsfiddle.net/QHac8/1/
As it's javascript you have to escape the forward slashes as they are the delimiters for a regex string.
or if you want to match any number:
/\/\d+\//
You don't check for a digit surrounded by slashes. The slashes you see are only your regex delimiters. You check for a 2 with a word boundary \b on each side. This is true for /2/ but also for =2
If you want to allow only a 2 surrounded by slashes try this
/^(.*?)\/2\/(.*?)$/
^ means match from the start of the string
$ match till the end of the string
(.*?) those parts are matching everything before and after your 2 and those parts are stored in capturing groups.
If you don't need those parts, then Richard D is right and the regex /\/2\// is fine for you.

Categories

Resources