Regex for specific strings/paths - javascript

I need a regular expression to match many specific paths/strings but I can't figure it out.
E.g.
../foo/hoo/something.js -> Needs to match ../foo/hoo/
../foo/bar/somethingElse.js -> Needs to match ../foo/bar/
../foo/something-else.js -> Needs to match ../foo/
What I tried with no luck is the following regex:
/\..\/foo\/|bar\/|hoo\//g

This should work out for you:
/(\.\.\/foo\/(hoo\/|bar\/)?)/
https://regex101.com/r/1aTf7y/1
So you select ../foo/ at first and then have a group that can either contain hoo/ or bar/. And the question mark allows 0 or one instances.
If you want to be a little less specific, you could also do
/(\.\.\/[^\/]+\/(hoo\/|bar\/)?)/
The [^\/]+ allows all characters except for a slash

You can use the regex
(\/[^\/\s]+)+(?=\/)
see the regex101 demo
function match(str){
console.log(str.match(/(\/[^\/\s]+)+(?=\/)/)[0]);
}
match('./foo/hoo/something.js');
match('../foo/bar/somethingElse.js');
match('../foo/something-else.js');

This should be the regex for matching all dirs without filename.
/^(.*[/])[^/]+$/

Related

Replace Numbers with dots

I am trying to replace some ID numbers in my system to clickable number to open the related record. The problem is, that they are sometimes in this format: 123.456.789.
When I use my regex, I can replace them and it works fine. The problem accurse when I also have IP addresses where the regex also matches: 123.[123.123.123] (the [] indicates where it matches).
How I can I prevent this behavior?
I tried something like this: /^(?!\.)([0-9]{3}\.[0-9]{3}\.[0-9]{3})(?!\.)/
I am working on "notes" in a ticket system. When the note contains only the ID or an IP, the regexp is working. When it contains more text like:
Affected IDs:
641.298.855 (this, lead)
213.794.868
948.895.285
Then it is not matching anymore on my IDs. Could you help me with this issue and explain what I am doing wrong?
Add gm modifier:
/^(?!\.)([0-9]{3}\.[0-9]{3}\.[0-9]{3})(?!\.)/gm
https://regex101.com/r/pK1fV4/2
You don't need to use negative lookahead at the start and also you don't need to include g modifier, just m modifier would be enough for this case because ^ matches the start of a line and the following pattern will match the string which exists only at the start so it won't do any global match (ie, two or more matches in a single line).
/^([0-9]{3}\.[0-9]{3}\.[0-9]{3})(?!\.)/m
For the sake of performance, you further don't need to use capturing group.
/^[0-9]{3}\.[0-9]{3}\.[0-9]{3}(?!\.)/m

javascript regexp to match path depth

Been struggling for the last hour to try and get this regexp to work but cannot seem to crack it.
It must be a regexp and I cannot use split etc as it is part of a bigger regexp that searches for numerous other strings using .test().
(public\/css.*[!\/]?)
public/css/somefile.css
public/css/somepath/somefile.css
public/css/somepath/anotherpath/somefile.css
Here I am trying to look for path starting with public/css followed by any character except for another forward slash.
so "public/css/somefile.css" should match but the other 2 should not.
A better solution may be to somehow specify the number of levels to match after the prefix using something like
(public\/css\/{1,2}.*)
but I can't seem to figure that out either, some help with this would be appreciated.
edit
No idea why this question has been marked down twice, I have clearly stated the requirement with sample code and test cases and also attempted to solve the issue, why is it being marked down ?
You can use this regex:
/^(public\/css\/[^\/]*?)$/gm
^ : Starts with
[^/] : Not /
*?: Any Characters
$: Ends with
g: Global Flag
m: Multi-line Flag
Something like this?
/public\/css\/[^\/]+$/
This will match
public/css/[Any characters except for /]$
$ is matching the end of the string in regex.

Javascript regex optional on left or right

Hi I can't get my pattern to work correctly. I want to detect if a specific word has any word/letter either in left or right side or both.
For example:
a{placeholder} = found
{placeholder}b = found
a{placeholder}b = found
{placeholder} = not found
This is my pattern so far (\w)?\{LINK_TO_WEB_VERSION\}(\w)?
https://regex101.com/r/hX4lM0/1
You need to explicitly define the patterns combined with the delimiter |
\w\{LINK_TO_WEB_VERSION\}\w?|\w?\{LINK_TO_WEB_VERSION\}\w|\w\{LINK_TO_WEB_VERSION\}\w
DEMO
You can use this negative lookahead based regex:
/^(?!\B{LINK_TO_WEB_VERSION}\B).+$/gim
RegEx Demo
You have more options
1: with an or condition (|) you can say, match all with letter before or letter after as follows: /(\w){placeholder}|{placeholder}(\w)/img
if you have one of them, is found, you not need match before AND after ;)
2: negate all stuff: find placeholder with no letter before and no letter after: /[^A-Z]{placeholder}[^A-Z]/img => not found, any other case mean found
You not need look behind or look ahead, in my opinion, but you can use it if you want: /(?<!\w)\{placeholder\}(?!\w)/ , but be careful, look behind is not supported by some languages (f.e. javascript).

What's wrong with this regular expression to find URLs?

I'm working on a JavaScript to extract a URL from a Google search URL, like so:
http://www.google.com/search?client=safari&rls=en&q=thisisthepartiwanttofind.org&ie=UTF-8&oe=UTF-8
Right now, my code looks like this:
var checkForURL = /[\w\d](.org)/i;
var findTheURL = checkForURL.exec(theURL);
I've ran this through a couple regex testers and it seems to work, but in practice the string I get returned looks like this:
thisisthepartiwanttofind.org,.org
So where's that trailing ,.org coming from?
I know my pattern isn't super robust but please don't suggest better patterns to use. I'd really just like advice on what in particular I did wrong with this one. Thanks!
Remove the parentheses in the regex if you do not process the .org (unlikely since it is a literal). As per #Mark comment, add a + to match one or more characters of the class [\w\d]. Also, I would escape the dot:
var checkForURL = /[\w\d]+\.org/i;
What you're actually getting is an array of 2 results, the first being the whole match, the second - the group you defined by using parens (.org).
Compare with:
/([\w\d]+)\.org/.exec('thisistheurl.org')
→ ["thisistheurl.org", "thisistheurl"]
/[\w\d]+\.org/.exec('thisistheurl.org')
→ ["thisistheurl.org"]
/([\w\d]+)(\.org)/.exec('thisistheurl.org')
→ ["thisistheurl.org", "thisistheurl", ".org"]
The result of an .exec of a JS regex is an Array of strings, the first being the whole match and the subsequent representing groups that you defined by using parens. If there are no parens in the regex, there will only be one element in this array - the whole match.
You should escape .(DOT) in (.org) regex group or it matches any character. So your regex would become:
/[\w\d]+(\.org)/
To match the url in your example you can use something like this:
https?://([0-9a-zA-Z_.?=&\-]+/?)+
or something more accurate like this (you should choose the right regex according to your needs):
^https?://([0-9a-zA-Z_\-]+\.)+(com|org|net|WhatEverYouWant)(/[0-9a-zA-Z_\-?=&.]+)$

Match a specific sequence or everything else with regex

Been trying to come up with a regex in JS that could split user input like :
"Hi{user,10,default} {foo,10,bar} Hello"
into:
["Hi","{user,10,default} ","{foo,10,bar} ","Hello"]
So far i achieved to split these strings with ({.+?,(?:.+?){2}})|([\w\d\s]+) but the second capturing group is too exclusive, as I want every character to be matched in this group. Tried (.+?) but of course it fails...
Ideas fellow regex gurus?
Here's the regex I came up with:
(:?[^\{])+|(:?\{.+?\})
Like the one above, it includes that space as a match.
Use this:
"Hi{user,10,default} {foo,10,bar} Hello".split(/(\{.*?\})/)
And you will get this
["Hi", "{user,10,default}", " ", "{foo,10,bar}", " Hello"]
Note: {.*?}. The question mark here ('?') stops at fist match of '}'.
Beeing no JavaScript expert, I would suggest the following:
get all positive matches using ({[^},]*,[^},]*,[^},]*?})
remove all positive matches from the original string
split up the remaining string
Allthough, this might get tricky if you need the resulting values in order.

Categories