regex: replace dynamically a searched word - javascript

I want to replace file:///Downloads/project by a another word like a github link :
const string = 'file:///Downloads/project/users/controllers/users/controller.js';
I tried working with .replace a world by a world but i got in the problem that file:///Downloads/projectis a dynamic value that may change from time to time.
string.replace(/file\:\/\/Downloads\/project\/users\/controller.js/gi, 'https://gitlab.com')
So i want to search for the word project and replace from it backward by another path or word

To achieve expected result, use below option of using indexOf and substr and finally replacing with gitlab url
Get index of project
Get string from 0 to index of project
Replace string from step 2 with gitlab using replace option
const string = 'file:///Downloads/project/users/controllers/users/controller.js';
const index = string.indexOf('project')
console.log(string.replace(string.substr(0, index), 'https://gitlab.com/'))
codepen - https://codepen.io/nagasai/pen/qvjdEa?editors=1010

Using regex you can match the first group which includes /project and replace the first parenthesized capture group with your https://gitlab.com. Here p1 denotes first parenthesized capture group and p2 denotes second parenthesized capture group.
const str = 'file:///Downloads/project/users/controllers/users/controller.js';
const res = str.replace(/^(.|\/)+\w*project(.+)*/gi, function(match, p1, p2) {
return 'https://gitlab.com' + p2;
});
console.log(res);

you don't need a regex for that
const string = 'file:///Downloads/project/users/controllers/users/controller.js',
yourValue = "file:///Downloads/project/",
res = string.replace(yourValue, "https://gitlab.com/")
console.log(res)

'https://gitlab.com/' + string.substr(string.indexOf('project'))
First find the position of 'project' in your string
string.indexOf('project')
then get a substring from that position to the end of the string (substr goes till the end when no second arg is provided)
string.substring(indexOfProject)
then concat it with '+'. Note that the url ends with '/' because your string will begin with 'p'.
'https://gitlab.com/' + extractedString

Related

Filter last slash of URL and return the last character

I can get the last character of the last part of the URL by using the split method and pop method such as
.split("/").pop() .
But usually, there is a slash at the end of the URL like so: https://exmaple.com/123/456/
It will return empty because there is simply nothing after the last slash.
How do I get the number '6' that is in the last part of the URL in the best and easy way?
One option would be to just remove the slashes, and return the last character no matter what it is.
Here's a quick example:
const getLastChar = (str) =>
str.replaceAll('/', '').slice(-1);
// Example #1 - Trailing slash
console.log(getLastChar('https://exmaple.com/123/456/'));
// Example #2 - No trailing slash
console.log(getLastChar('https://exmaple.com/123/456'));
If it's based on user input, don't forget to also check that the string is present / defined too :)
Just Remove the last "/" Like this:
let str = "https://example.com/abc/123/";
//if the str has a "/" at the end, remove it
if (str.endsWith("/")) {
str = str.slice(0, -1);
}
// str = "https://example.com/abc/123"
console.log(str);

How can I cut the string after a second underscore?

I'm receiving a list of files in an object and I just need to display a file name and its type in a table.
All files come back from a server in such format: timestamp_id_filename.
Example: 1568223848_12345678_some_document.pdf
I wrote a helper function which cuts the string.
At first, I did it with String.prototype.split() method, I used regex, but then again - there was a problem. Files can have underscores in their names so that didn't work, so I needed something else. I couldn't come up with a better idea. I think it looks really dumb and it's been haunting me the whole day.
The function looks like this:
const shortenString = (attachmentName) => {
const file = attachmentName
.slice(attachmentName.indexOf('_') + 1)
.slice(attachmentName.slice(attachmentName.indexOf('_') + 1).indexOf('_') + 1);
const fileName = file.slice(0, file.lastIndexOf('.'));
const fileType = file.slice(file.lastIndexOf('.'));
return [fileName, fileType];
};
I wonder if there is a more elegant way to solve the problem without using loops.
You can use replace and split, with the pattern we are replacing the string upto the second _ from start of string and than we split on . to get name and type
let nameAndType = (str) => {
let replaced = str.replace(/^(?:[^_]*_){2}/g, '')
let splited = replaced.split('.')
let type = splited.pop()
let name = splited.join('.')
return {name,type}
}
console.log(nameAndType("1568223848_12345678_some_document.pdf"))
console.log(nameAndType("1568223848_12345678_some_document.xyz.pdf"))
function splitString(val){
return val.split('_').slice('2').join('_');
}
const getShortString = (str) => str.replace(/^(?:[^_]*_){2}/g, '')
For input like
1568223848_12345678_some_document.pdf, it should give you something like some_document.pdf
const re = /(.*?)_(.*?)_(.*)/;
const name = "1568223848_12345678_some_document.pdf";
[,date, id, filename] = re.exec(name);
console.log(date);
console.log(id);
console.log(filename);
some notes:
you want to make the regular expression 1 time. If you do this
function getParts(str) {
const re = /expression/;
...
}
Then you're making a new regular expression object every time you call getParts.
.*? is faster than .*
This is because .* is greedy so the moment the regular expression engine sees that it puts the entire rest of the string into that slot and then checks if can continue the expression. If it fails it backs off one character. If that fails it backs off another character, etc.... .*? on the other hand is satisfied as soon as possible. So it adds one character then sees if the next part of the expression works, if not it adds one more character and sees if the expressions works, etc..
splitting on '_' works but it could potentially make many temporary strings
for example if the filename is 1234_1343_a________________________.pdf
you'd have to test to see if using a regular experssion is faster or slower than splitting, assuming speed matters.
You can kinda chain .indexOf to get second offset and any further, although more than two would look ugly. The reason is that indexOf takes start index as second argument, so passing index of the first occurrence will help you find the second one:
var secondUnderscoreIndex = name.indexOf("_",name.indexOf("_")+1);
So my solution would be:
var index = name.indexOf("_",name.indexOf("_")+1));
var [timestamp, name] = [name.substring(0, index), name.substr(index+1)];
Alternatively, using regular expression:
var [,number1, number2, filename, extension] = /([0-9]+)_([0-9]+)_(.*?)\.([0-9a-z]+)/i.exec(name)
// Prints: "1568223848 12345678 some_document pdf"
console.log(number1, number2, filename, extension);
I like simplicity...
If you ever need the date in times, theyre in [1] and [2]
var getFilename = function(str) {
return str.match(/(\d+)_(\d+)_(.*)/)[3];
}
var f = getFilename("1568223848_12345678_some_document.pdf");
console.log(f)
If ever files names come in this format timestamp_id_filename. You can use a regular expression that skip the first two '_' and save the nex one.
test:
var filename = '1568223848_12345678_some_document.pdf';
console.log(filename.match(/[^_]+_[^_]+_(.*)/)[1]); // result: 'some_document.pdf'
Explanation:
/[^]+[^]+(.*)/
[^]+ : take characters diferents of ''
: take '' character
Repeat so two '_' are skiped
(.*): Save characters in a group
match method: Return array, his first element is capture that match expression, next elements are saved groups.
Split the file name string into an array on underscores.
Discard the first two elements of the array.
Join the rest of the array with underscores.
Now you have your file name.

Javascript regex between string delimiters

I have the following string:
%||1234567890||Joe||% some text winter is coming %||1234567890||Robert||%
PROBLEM: I am trying to match all occurrences between %||....||% and process those substring matches
MY REGEX: /%([\s\S]*?)(?=%)/g
MY CODE
var a = "%||1234567890||Joe||% some text winter is coming %||1234567890||Robert||%";
var pattern = /%([\s\S]*?)(?=%)/g;
a.replace( pattern, function replacer(match){
return match.doSomething();
} );
Now the patterns seems to be selecting the everything between the first and last occurrence of %|| .... %||
MY
FIDDLE
WHAT I NEED:
I want to iterate over the matches
%||1234567890||Joe||%
AND
%||1234567890||Robert||%
and do something
You need to use a callback inside a String#replace and modify the pattern to only match what is inside %|| and ||% like this:
var a = "%||1234567890||Joe||% some text winter is coming %||1234567890||Robert||%";
var pattern = /%\|\|([\s\S]*?)\|\|%/g;
a = a.replace( pattern, function (match, group1){
var chunks = group1.split('||');
return "{1}" + chunks.join("-") + "{/1}";
} );
console.log(a);
The /%\|\|([\s\S]*?)\|\|%/g pattern will match:
%\|\| - a %|| substring
([\s\S]*?) - Capturing group 1 matching any 0+ chars as few as possible up to the first...
\|\|% - a ||% substring
/g - multiple times.
Because he tries to take as much as possible, and [\s\S] basically means "anything". So he takes anything.
RegExp parts without escaping, exploded for readability
start tag : %||
first info: ([^|]*) // will stop at the first |
separator : ||
last info : ([^|]*) // will stop at the first |
end tag : ||%
Escaped RegExp:
/%\|\|([^\|]*)\|\|([^\|]*)\|\|%/g

javascript finding part of a string

I'm not very used to Javascript so I'm having trouble manipulating strings...
If I have something like /folder1/folder2/folder3/ , how do I parse it so I end up with just the current folder, e.g. "folder3" ?
Thanks!
var folderName = str.match(/(folder\d+)\/$/)[1];
Should do it.
Explanation of the regex:
( -> Start of capture group. We want a capture group because we just want
the folder name without the trailing slash.
folder -> Match the string "folder"
\d+ -> Match one or more digits
) -> End of capture group. This lets us capture strings of the form
"folderN", where N is a number.
\/ -> Escape forward slash. We have to escape this because / is used to
represent the start and end of a regex literal, in Javascript.
$ -> Match the end of the string.
The reason we are selecting the second element of the array (at index 1) is because the first element contains the complete string that was matched. This is not what we want; we just want the capture group. We only have one group that we captured, and so that is the second element.
Well, just because it's an option (though not necessarily sane):
var string = '/folder1/folder2/folder3/',
last = string.replace(/\//g,' ').trim().split(/\s/).pop();
console.log(last);
JS Fiddle demo.
You can use the split function to retrieve all subpaths:
var path = '/folder1/folder2/folder3/';
var paths = path.split('/');
var pathNeeded = paths[paths.length - 2];
Working example
How stable is the format of that string?
With a trailing slash you will need the next to last item
var parts = URL.split("/"); alert(parts[parts.length-2]);
var str = "/folder1/folder2/folder3/",
folder = str.substring(0, str.length - 1).split('/').pop();
FIDDLE
The split function transform your string into an array using the supplied parameter as a delimiter.
Therefore:
var parts = "/folder1/folder2/folder3/".split("/");
Will result in parts being equal to:
["", "folder1", "folder2", "folder3", ""]
You could then access each item using:
parts[0] // returns ''
parts[1] // returns 'folder1'
parts[2] // returns 'folder2'
.. and so on. Read more on split here:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/split
You can write the following:
var myString = '/fold1/fold2/fold3';
var myArray = myString.split('/');
var last_element = myArray[myArray.length - 1];
See the doc split

Javascript (node) regex doesn't seem to match start of string

im struggling with regular expressions in Javascript, they don't seem to start at the beginning of the string. In a simple example bellow I want to get the file name and then everything after the first colon
//string
file.text:16: lots of random text here with goes on for ages
//regex
(.?)[:](.*)
// group 1 returns 't'
/^([^:]+):(.*)/.exec('file.text:16: lots of random text here with goes on for ages')
gives ....
["file.text:16: lots of random text here with goes on for ages", "file.text", "16: lots of random text here with goes on for ages"]
Try this regex:
/^([^:]+)[:](.*)/
Explaination:
^ #Start of string
( #Start of capturing class #1
[^:] #Any character other than :
+ #One or more of the previous character class
) #End of capturing class #1
[:] #One :
(.*) #Any number of characters other than newline
The ? operator captures zero or one of the previous symbol only.
You could also use string operations instead:
str = "file.text:16:";
var n = str.indexOf(":");
var fileName = str.substr(0, n);
var everythingElse = str.substr(n);
The ? operator returns 0 or 1 matches. You want the * operator, and you should select everything that isn't a : in the first set
([^:]*)[:](.*)
Non-regexy answer:
var a = s.split(":");
Then join a[1] and remaining elements.
Or just get the index of the first semicolon and create two strings using that.

Categories

Resources