I'm trying to remove all the text which falls before the last character in a Regex pattern.
Example:
rom.com/run/login.php
Becomes:
login.php
How would I go about doing this in JavaScript? I'm new to regular expressions.
To get everything after last slash, use [^\/]+$
const str = "rom.com/run/login.php";
console.log(str.match(/[^/]+$/)[0]);
You can get the result you need by searching for a literal string (just one character in fact) so there's no need to employ regular expressions which will cost you performance.
You can split the input into chunks separated by / and get the last chunk:
var input = 'rom.com/run/login.php';
var result = input.split('/').pop();
Or find the position of the last occurrence of / in the input, and get the remainder of the string that follows that position:
var input = 'rom.com/run/login.php';
var result = input.substring(input.lastIndexOf('/') + 1);
One approach is a regex replacement:
var path = "rom.com/run/login.php";
var output = path.replace(/^.*\//, "");
console.log(output);
The regex pattern ^.*/ is greedy, and will consume everything up to (and including) the last path separator. Then, we replace this match with empty string, to effectively remove it.
You could do it with Regex like this:
var url = 'rom.com/run/login.php'
var page = url.match('^.*/(.*)')[1]
console.log(page)
Or you could do it without Regex like this:
var url = 'rom.com/run/login.php'
var split = url.split('/')
var page = split[split.length-1]
console.log(page)
I am relatively new to RegEx and am trying to achieve something which I think may be quite simple for someone more experienced than I.
I would like to construct a snippet in JavaScript which will take an input and strip anything before and including a specific character - in this case, an underscore.
Thus 0_test, 1_anotherTest, 2_someOtherTest would become test, anotherTest and someOtherTest, respectively.
Thanks in advance!
You can use the following regex (which can only be great if your special character is not known, see Alex's solution for just _):
^[^_]*_
Explanation:
^ - Beginning of a string
[^_]* - Any number of characters other than _
_ - Underscore
And replace with empty string.
var re = /^[^_]*_/;
var str = '1_anotherTest';
var subst = '';
document.getElementById("res").innerHTML = result = str.replace(re, subst);
<div id="res"/>
If you have to match before a digit, and you do not know which digit it can be, then the regex way is better (with the /^[^0-9]*[0-9]/ or /^\D*\d/ regex).
Simply read from its position to the end:
var str = "2_someOtherTest";
var res = str.substr(str.indexOf('_') + 1);
I am writing a google apps script that pulls the content from a CSV file in a gmail attachment. I need to split the csv data into several an array (with each row being it's own array). I've got all that down.
My problem is this: One of the values in the CSV is a "City, St" format. So when I split the array using string.split(',') I end up with an extra "column." My idea to fix this was to back up and kill that comma in the initial string. Here's the relevant portion of my code:
var attachments = msgs[i][j].getAttachments();
for (var k = 0; k < attachments.length; k++) {
var attachmentData = attachments[k].getDataAsString();
var regex = new RegExp('\,\s(?:[A-Z]{2}\")', 'gi');
attachmentData.replace(regex,' ');
...
So what I'm trying to do is just find a comma, followed by a space, followed by exactly two letters, then a quotation mark. I want to just replace the comma with a space. I've also tried
var regex = new RegExp('(\,\s)([A-Z]{2}\")', 'gi');
attachmentData.replace(regex,$2);
with no luck. Here's a random sample of the (very long) data string I'm running this on:
Voice,Incoming,(###) ###-####,(###) ###-####,,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00:27,$0.000,-,,
,Incoming,(###) ###-####,,###,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00
Can anyone see what I'm not seeing as to why this isn't working? (Or have any ideas of a better way to do this?)
The replace() method returns a new string with some or all matches
of a pattern replaced by a replacement.
The str.replace does not change the string, where as returns a new string with the replace. Hence you may want to write something like
var regex = new RegExp('(,\\s)([A-Z]{2}")', 'gi');
var replacedData = attachmentData.replace(regex,'$2');
Note
You can drop the first capture group as
var regex = new RegExp(',\\s([A-Z]{2}")', 'gi');
var replacedData = attachmentData.replace(regex,'$1');
You can make use of a regex with this condition and then print back the block with $1 together with the space:
s = s.replace(/,( [A-Z]{2}")/, ' $1');
^^ ^ ^^^
comma ^^^^^^^^^ print back replacing comma with space
|
catch the group of space + two letters + "
See it live:
var s = 'Voice,Incoming,(###) ###-####,(###) ###-####,,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00:27,$0.000,-,, ,Incoming,(###) ###-####,,###,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00';
s = s.replace(/,( [A-Z]{2})/, ' $1');
document.write(s)
I have snippet javascript (web) but since i try to use RegExpression it should work almost the same.
I have a string with some coordinates in it seperated by a space charakter (last coordinate has no space after it).
var coords = "0:0 0:0:0 1:0:1 0:0:0:1 0:0:1 0:0:2";
var part = "0:0";
I want to have all the coordinates beginning with the value of part ("0:0") plus ":" and the next coordinate number. If a coordinate matches but has more than one additional "coordinate-dimensions" it shouldn't show...
For example it should show 0:0:0, 0:0:1 and 0:0:2 but NOT 0:0 (because to less dimension), 0:0:0:1 (because if the additional dimension)
What I tryed is something like:
var reg = new RegExp("(^|\\s)(0:0:\\d\\s)", "g");
alert(coords .match(reg));
But it seems not to work propperly.
Anyone has an idea?!
Kind regards!
You can use this regex:
"(?:^|\\s)(0:0:\\d)(?=\\s|$)"
So what I want to match is anything that ends with ".ProjectName" so I wrote a small test case. I purposely created the pattern using RegExp because in the real case scenario I will be using a variable as part of the reg ex pattern. I'm not sure if my pattern is not correct (90% sure it correct), or if I am misusing the match function (70% sure I am suing it right). The blow code returns me something when the second case notMatchName should not return me anything
var inputName = "ProjectName";
var matchName = "userInput_Heading.Heading.ProjectName";
var notMatchName = "userInput_Heading.Heading.Date";
var reg = new RegExp(".*[." + inputName + "]");
console.log(reg);
console.log(matchName.match(reg));
console.log(matchName.match(reg)[0]);
console.log(notMatchName.match(reg));
console.log(notMatchName.match(reg)[0]);
Here is the JsFiddle to help.
Use
var reg = new RegExp(".*\." + inputName);
The square brackets mean: one character, which is one of those within the brackets. But you want several characzters, first a dot, then the first character of inputName, etc.
your regular expression should be .*\.projectName
if you rewrite your statement it will be
var reg = new RegExp(".*\." + inputName)