Splitting a path using containing \ and / using a RegExp - javascript

I want to split a path containing forward and/or backward slashes into parts using Google Apps script.
I prefer doing this by using a single RegExp instead of writing much code.
so code should look like
var myPath = "<something>"; // i.e. \first\second/third\filename.extension
var parts = myPath.replace(<a regular expression>).split('/');
Allthough I searched SO, I didn't find a solution.
What should <a regular expression> be?

If you're just going to split, there's no need to replace beforehand then split. You can pass the regex to the split function directly. Like this:
var parts = mPath.split(/[/\\]/);

Related

extract last portion from url

Having the following link:
/C:\Users\xxx\Desktop\yyy\public\assets\1634648850202.jpg
How do i extract 1634648850202.jpg?
i tried:
const lastplace = thisUrl.substring(thisUrl.lastIndexOf("\"));
This does not work, because the backslash is recognized as a functional character (is this the right term?)
Here are 2 Questions from my side:
how do i extract the last part?
how should i handle backslashes in general?
Common way:
thisUrl.split(/\/|\\/).pop() // 1634648850202.jpg
I also recommend to split url/path by both slashes (/,\) 'cause of different enviromnents/systems uses diffrent character.
UPD: just make your \ double to handle it (you're right, it's a escape character), like this:
''.lastIndexOf('\\')
Also you need do the same when you initiate your string:
const thisUrl = '/C:\\Users\\xxx\\Desktop\\yyy\\public\\assets\\1634648850202.jpg';

JavaScript RegEx match unless wrapped with [nocode][/nocode] tags

My current code is:
var user_pattern = this.settings.tag;
user_pattern = user_pattern.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); // escape regex
var pattern = new RegExp(user_pattern.replace(/%USERNAME%/i, "(\\S+)"), "ig");
Where this.settings.tag is a string such as "[user=%USERNAME%]" or "#%USERNAME%". The code uses pattern.exec(str) to find any username in the corresponding tag and works perfectly fine. For example, if str = "Hello, [user=test]" then pattern.exec(str) will find test.
This works fine, but I want to be able to stop it from matching if the string is wrapped in [nocode][/nocode] tags. For example, if str = "[nocode]Hello, [user=test], how are you?[/nocode]" thenpattern.exec(str)` should not match anything.
I'm not quite sure where to start. I tried using a (?![nocode]) before and after the pattern, but to no avail. Any help would be great.
I would just test if the string starts with [nocode] first:
/^\[nocode\]/.test('[nocode]');
Then simply do not process it.
Maybe filter out [nocode] before trying to find the username(s)?
pattern.exec(str.replace(/\[nocode\](.*)\[\/nocode\]/g,''));
I know this isn't exactly what you asked for because now you have to use two separate regular expressions, however code readability is important too and doing it this way is definitely better in that aspect. Hope this helps 😉
JSFiddle: http://jsfiddle.net/1f485Lda/1/
It's based on this: Regular Expression to get a string between two strings in Javascript

How can i remove http://webservices.rki.dk this in xml

I want to remove namespace in xml.Can you please write regular expression in javascript for the following two strings.I want these two strings in my whole xml.
xmlns="http://webservices.rki.dk"
xmlns="http://webservices.rki.dk/"
You want to use string.replace() to get rid of those. See here.
Example to globally replace in a string (You need to escape some chars to make it work):
var str1 = 'xmlns="http://webservices.rki.dk" hello xmlns="http://webservices.rki.dk"'
var str1Fixed = str1.replace(/xmlns=\"http:\/\/webservices.rki.dk\"/g,"");
alert(str1Fixed);

How to end a regular expression with a forward slash in Javascript

Problem
I am trying to match the hash part of a URL using Javascript. The hash will have the format
/#\/(.*)\//
This is easy to achieve using "new RegExp()" method of creating a JS regular expression, but I can't figure out how to do it using the standard format, because the two forward slashes at the end begin a comment. Is there another way to write this that won't start a comment?
Example
// works
myRegexp = new RegExp ('#\/(.*)\/');
// fails
myRegexp = /#\/(.*)\//
I am trying to match the hash part of a URL using Javascript.
Yeah, don't do that. There's a perfectly good URL parser built into every browser. Set an href on a location object (window.location or a link) and you can read/write URL parts from properties hostname, pathname, search, hash etc.
var a= document.createElement('a');
a.href= 'http://www.example.com/foo#bar#bar';
alert(a.hash); // #bar#bar
If you're putting a path-like /-separated list in the hash, I'd suggest hash.split('/') to follow.
As for the regex, both versions work identically for me. The trailing // does not cause a comment. If you just want to appease some dodgy syntax highlighting, you could potentially escape the / to \x2F.
It is not starting a comment, just like two slashes in a string. Look here: http://jsfiddle.net/Gr2qb/2/

Javascript storing regex in a variable

I have this line of code in javascript
var re = (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?
Usually I encapsulate the regex syntax with the / characters but since they are found within the regex it screws up the encapsulation. Is there another way how I can store it inside the variable?
The current slashes that seem like escape characters are part of the regex, since I am using this in c# aswell and works perfectly
var re = new RegExp("^your regexp.*$", "gi");
One way is to escape all occurances of / in your regex as \/, like you're already partially doing:
var re = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/;
You can escape the slash inside your regex:
/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/
(you already did so with the first two slashes...)

Categories

Resources