RegExp Match only with paths contains filename - javascript

I am trying to create a Regexp in JS that only look into string having files and ignore the paths which don't have a filename.
input
tanxe\src\lib\hello
tanxe\lib\hello\world\verboseBackup.js
tanxe\src\hello\verboseBackup.js
tanxe\lib\verboseBackup.js
Trying
input.match(/^tanxe.*?lib.*?\\..*/i)
Expected Output:
tanxe\lib\hello\world\verboseBackup.js
tanxe\lib\verboseBackup.js

You can try this mate
^.*\.\w+$
Explanation
^ - Anchor to start of string.
.* - Matches anything one or more times except newline character.
\. - Matches ..
\w+ - Matches word character one or more time.
$ - End of string.
Demo
Update:- In case you strictly want to match tanxe\lib and followed things only
You can try this mate
^tanxe\\lib\\.+\.\w+$
Demo

You might try this: tanxe\\lib.*?\.\w+
It matches paths starting with tanxe\lib and ending with a file extension.

input.match(/^tanxe\\lib\\(\w+\\)*\w+\.\w+/gi);
See the regExr fiddle I created.

Your regex is work, I think you need is additional flags: g global, m multiline
var input = `tanxe\\src\\lib\\hello
tanxe\\lib\\hello\\world\\verboseBackup.js
tanxe\\src\\hello\\verboseBackup.js
tanxe\\lib\\verboseBackup.js
D:\\Program Files\\atom\\.atom\\packages\\utilities-tanxe\\lib\\abc\\verboseBackup.js`
input.match(/^.*tanxe.*?lib.*?\..*/gmi).forEach(r => console.log(r))
// start with "tanxe"
//input.match(/^tanxe.*?lib.*?\..*/gmi).forEach(r => console.log(r))

Try this one too.
tanxe\\[a-zA-Z\\]+[.]{1}[a-zA-z]{2,3}

Related

Regex: strip out if not "OBX"

I have data like this:
MSH|1|data1|data2|data3
PID|1|data5|data6|data7
PVI|1|data2|data2|data2
OBX|1|data0|data4|data9
OBX|2|data8|data8|data9
OBX|3|data1|data1|data1
I am trying regex to strip out any lines that don't start with OBX. Here's what I have so far:
message = message.replace(/^(?!OBX).+/g, '');
Even though I have /g it only triggered on the first. Is there something else I'm missing?
Since you are using anchor ^ in your regex, you will need to use m flag (MULTILINE):
message = message.replace(/^(?!OBX).+/gm, '');
Without m modifier your regex will match ^ only at the start of very first line instead of matching before every line.
RegEx Demo

RegEx - Get All Characters After Last Slash in URL

I'm working with a Google API that returns IDs in the below format, which I've saved as a string. How can I write a Regular Expression in javascript to trim the string to only the characters after the last slash in the URL.
var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9'
Don't write a regex! This is trivial to do with string functions instead:
var final = id.substr(id.lastIndexOf('/') + 1);
It's even easier if you know that the final part will always be 16 characters:
var final = id.substr(-16);
A slightly different regex approach:
var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];
Breaking down this regex:
\/ match a slash
( start of a captured group within the match
[^\/] match a non-slash character
+ match one of more of the non-slash characters
) end of the captured group
\/? allow one optional / at the end of the string
$ match to the end of the string
The [1] then retrieves the first captured group within the match
Working snippet:
var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9';
var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];
// display result
document.write(afterSlashChars);
Just in case someone else comes across this thread and is looking for a simple JS solution:
id.split('/').pop(-1)
this is easy to understand (?!.*/).+
let me explain:
first, lets match everything that has a slash at the end, ok?
that's the part we don't want
.*/ matches everything until the last slash
then, we make a "Negative lookahead" (?!) to say "I don't want this, discard it"
(?!.*) this is "Negative lookahead"
Now we can happily take whatever is next to what we don't want with this
.+
YOU MAY NEED TO ESCAPE THE / SO IT BECOMES:
(?!.*\/).+
this regexp: [^\/]+$ - works like a champ:
var id = ".../base/nabb80191e23b7d9"
result = id.match(/[^\/]+$/)[0];
// results -> "nabb80191e23b7d9"
This should work:
last = id.match(/\/([^/]*)$/)[1];
//=> nabb80191e23b7d9
Don't know JS, using others examples (and a guess) -
id = id.match(/[^\/]*$/); // [0] optional ?
Why not use replace?
"http://google.com/aaa".replace(/(.*\/)*/,"")
yields "aaa"

How can I get the last part of a songkick URL using regex?

I need to get the last part after the slash from this URL or even just the number using regex:
http://www.songkick.com/artists/2884896-netsky
Does anyone know how I can go about doing this?
Many thanks,
Joe
I've got a regex pattern to find everything after the last slash - it is ([^//]+)$ . Thanks!
don't know if this is ok for you:
last part:
"http://www.songkick.com/artists/2884896-netsky".replace(/.*\//,"")
"2884896-netsky"
number:
"http://www.songkick.com/artists/2884896-netsky".replace(/.*\//,"").match(/\d+/)
["2884896"]
Try this:
var s = "http://www.songkick.com/artists/2884896-netsky";
s.match(/[^\/]+$/)[0]
[^\/]+ matches one or more characters other than /
$ matches the end of the string
For just the number, try:
var value = s.match(/(\d+)[^\/\d]+$/)[1];
value = parseInt(value, 10); // convert it to an Integer
(\d+) matches any Number and groups it
[^\/\d]+ matches anything besides Numbers or /
More Info on Javascript Regular Expressions

Javascript Regex Replace - Not replacing all the required strings

I have no idea what is happening here..
model.attributes.data.Path.replace('/\\/g',""), #options.path.replace('/\\/g',"")
When doing :
console.log model.attributes.data.Path.replace('/\\/g',""),
#options.path.replace('/\\/g',"")
the data is:
T2/T2_2, T2/T2_2
It returns this:
T2T2_2, T2/T2_2
So only the first path was replaced, but not the second one? Why would that be?
Aside from the fact you're matching backslashes (\\ = \), instead of forward slashes (\/ = /), Don't put your regexes into the replace function as strings.
Use:
.replace(/\//g,"");
Instead of
.replace('/\//g',"");
Then it'll work just fine:
"T2/T2_2 , T2/T2_2".replace(/\//g,"");
// returns: "T2T2_2 , T2T2_2"
Otherwise, it'll just try to literally find the string '/\//g'.
Also, to replace both forward and backslashes in 1 regex, try this:
"T2/T2_2 , T2\T2_2".replace(/\/|\\/g,"");
// returns: "T2T2_2 , T2T2_2"
# \/|\\ Matches:
# \/ - Forward slash
# | - Or
# \\ - Backslash
Try .replace(/\//g,"") instead of .replace('/\\/g',""), (the regex is not a string).
Try:
model.attributes.data.Path.replace(/\//g,"")
#options.path.replace(/\//g,"")
/\\/g matches a backslash and /\//g matches a forward slash.

what's wrong with this regular expression? getting the hash part of an url

I´m trying to get the first part of a hash from a url (the part between the # and a /, a ? or the end of the string
So far now I came out with this:
r = /#(.*)[\?|\/|$]/
// OK
r.exec('http://localhost/item.html#hash/sub')
["#hash/", "hash"]
// OK
r.exec('http://localhost/item.html#hash?sub')
["#hash?", "hash"]
// WAT?
r.exec('http://localhost/item.html#hash')
null
I was expeting to receive "hash"
I tracked down the problem to
/#(.*)[$]/
r2.exec('http://localhost/item.html#hash')
null
any idea what could be wrong?
r = /#(.*)[\?|\/|$]/
When $ appears in [] (character class, it's the literal "$" character, not the end of input/line. In fact, your [\?|\/|$] part is equivalent to just [?/$|], which matches the 4 specific characters (including pipe).
Use this instead (JSFiddle)
r = /#(.+?)(\?|\/|$)/
You aren't supposed to write [$] (within a character class) unless you want to match the $ literally and not the end of line.
/#(.*)$/
Code:
var regex = /\#(.*)$/;
regex.exec('http://localhost/item.html#hash');
Output:
["#hash", "hash"]
Your regex: /#(.*)[\?|\/|$]/
//<problem>-----^ ^-----<problem>
| operator won't work within [], but within ()
$ will be treated literally within []
.* will match as much as possible. .*? will be non-greedy
On making the above changes,
you end up with /#(.*?)(\?|\/|$)/
I use http://regexpal.com/ to test my regular expressions.
Your problem here is that your regular expression wants a /. So it don't works with http://localhost/item.html#hash but it works with http://localhost/item.html#hash/
Try this one :
r = /#([^\?|\/|$]*)/
You can't use the $ end-of-string marker in a character class. You're probably better off just matching characaters that aren't / or ?, like this:
/#([^\?\/]*)/
Why Regex? Do it like this (nearly no regex):
var a = document.createElement('a');
a.href = 'http://localhost/item.html#hash/foo?bar';
console.log(a.hash.split(/[\/\?]/)[0]); // #hash
Just for the sake, if it is node.js you are working with:
var hash = require('url').parse('http://localhost/item.html#hash').hash;
I found this regular expression that seems to work
r = /#([^\/\?]*)/
r.exec('http://localhost/item.html#hash/sub')
["#hash", "hash"]
r.exec('http://localhost/item.html#hash?sub')
["#hash", "hash"]
r.exec('http://localhost/item.html#hash')
["#hash", "hash"]
Anyway, I still don't get why the original one isn't working

Categories