Using regex to remove characters till the last - - javascript

I'm new to regex and trying to figure out how to remove characters till the last - in the string. I currently have strings in the format like this:
purple-hoodie.jpg-1625739747918
I am trying to remove characters to essentially be left with:
-1625739747918
Does anyone have any advice on how to approach this? I'm struggling to work out how to indicate to reach the last - in the string, if that is even possible?
Thanks

Just use lastIndexOf
let str = 'purple-hoodie.jpg-1625739747918'
console.log(str.substring(str.lastIndexOf('-')))

I prefer a match approach here:
var input = "purple-hoodie.jpg-1625739747918";
var output = input.match(/-\d+$/)[0];
console.log("match is: " + output);
But this assumes that the input would end in all digits. A more general regex approach might use a replace all:
var input = "purple-hoodie.jpg-1625739747918";
var output = input.replace(/^.*(?=-)/, "");
console.log("match is: " + output);

Here is my solution.
const txt = 'purple-hoodie.jpg-1625739747918';
const result = txt.replace(/-\d+$/, '');
console.log(result)
This removes the last trailing digits prefixed by -.

Related

RegEx for matching YouTube embed ID

I'm in non-modern JavaScript and I have a string defined as follows:
"//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0"
I want to pull out just the DmYK479EpQc but I don't know the length. I do know that I want what is after the / and before the ?
Is there some simple lines of JavaScript that would solve this?
Use the URL object?
console.log(
(new URL("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0", location.href)).pathname
.split('/')
.pop());
Why? Because I can likely make up a URL that defeats the regex (though for youtube it's probably unlikely)
This expression might help you to do so, and it might be faster:
(d\/)([A-z0-9]+)(\?)
Graph
This graph shows how the expression would work and you can visualize other expressions in this link:
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const str = `//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0`;
const subst = `$3`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Performance Test
This JavaScript snippet shows the performance of that expression using a simple 1-million times for loop.
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const string = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
var match = string.replace(regex, "$3");
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");
How about non-regex way
console.log("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0".split('/').pop().split('?')[0]);
I'm not going to give a piece of code because this is a relatively simple algorithm, and easy to implement.
Please note that those links has this format (correct me if I'm wrong):
https:// or http://
www.youtube.com/
embed/
Video ID (DmYK479EpQc in this case)
?parameters (note that they start ALWAYS with the character ?)
You want the ID of the video, so you can split the string into those sections and if you store those sections in one array you can be sure that the ID is at the 3rd position.
One example of how that array would look like would be:
['https://', 'www.youtube.com', 'embed', 'DmYK479EpQc', '?vq=hd720&rel=0']
One option uses a regex replacement:
var url = "//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0";
var path = url.replace(/.*\/([^?]+).*/, "$1");
console.log(path);
The above regex pattern says to:
.* match and consume everything up to and
/ including the last path separator
([^?]+) then match and capture any number of non ? characters
.* then consume the rest of the input
Then, we just replace with the first capture group, which corresponds to the text after the final path separator, but before the start of the query string, should the URL have one.
You can use this regex
.* match and consume everything up to
[A-z0-9]+ then match and capture any number and character between A-z
.* then consume the rest of the input
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const position = '$3';
let result = ytUrl.replace(regex, position);
console.log('YouTube ID: ', result);
This regex just split the string into different sections and the YouTube id is at the 3rd position.
Another, solution is using split. This method splits a string into an array of substrings.
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
let result = ytUrl.split('/').pop().split('?').shift()
console.log('YouTube ID: ', result);
In this sample, we split the URL using / as separator. Then we took the last element of the array with the pop method. and finally we split again using ? as separator and we take the first element of the array with the shift method.

Move letters to the most right possible

I will have one string mixed with dots and lowercase letters like:
string = '..a..d..h.w';
I want it to have all letters moved to the most right possible:
result = '.......adhw';
I would really appreciate a short solution for this ;)
I was trying to use regex:
result = string.replace(/(\.)|(.)/g,'$1$2');
But without luck, it remains the same.
Any help is welcome.
You could strip all of the dots and then strip all of the non-dots and concatenate them :
var output = input.replace(/[^\.]/g,'') + input.replace(/\./g,'');
A simple string manipulation for this could be string.count('.')*'.'+string.replace('.','')
you can try this:
var str = '..a..d..h.w';
var result = ("..........." + str.replace(/\./g, '')).slice(-str.length);

JavaScript - strip everything before and including a character

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);

Regex to extract all digits after last occurrence of a special character

I want to extract all digits after last occurrence of character "-" so for example 311-1974-8 should return me 8 and 311-1974-348 should return 348
edit:
Added clarification from a comment:
actually it's an external tool which provides it's own inbuild functionalists and i have no other option but to use regex to extract this. No JS can be applied :(
This captures the last number.
var str = '311-1974-348';
var matches = str.match(/-(\d+)$/);
var match = matches ? matches[1] : null;
console.log("matched? " + match);
Try matching on /[^-]+$/, e.g.:
var s = '311-1974-348';
s.match(/[^-]+$/); // => ["348"]
You mentioned in a comment it's for an external tool so...
-([0-9]+)$
dunno how your tool handles captured groups or anything...
Why not simply spliting ?
var str = input.split('-').pop();
Try this /[0-9]+$/. It worked on both the inputs you provided.

Searching for a last word in JavaScript

I am doing some logic for the last word that is on the sentence. Words are separated by either space or with a '-' character.
What is easiest way to get it?
Edit
I could do it by traversing backwards from the end of the sentence, but I would like to find better way
Try splitting on a regex that matches spaces or hyphens and taking the last element:
var lastWord = function(o) {
return (""+o).replace(/[\s-]+$/,'').split(/[\s-]/).pop();
};
lastWord('This is a test.'); // => 'test.'
lastWord('Here is something to-do.'); // => 'do.'
As #alex points out, it's worth trimming any trailing whitespace or hyphens. Ensuring the argument is a string is a good idea too.
Using a regex:
/.*[\s-](\S+)/.exec(str)[1];
that also ignores white-space at the end
Have you tried the lastIndexOf function http://www.w3schools.com/jsref/jsref_lastIndexOf.asp
Or Split function http://www.w3schools.com/jsref/jsref_split.asp
Here is a similar discussion have a look
You can try something like this...
<script type="text/javascript">
var txt = "This is the sample sentence";
spl = txt.split(" ");
for(i = 0; i < spl.length; i++){
document.write("<br /> Element " + i + " = " + spl[i]);
}
</script>
Well, using Split Function
string lastWord = input.Split(' ').Last();
or
string[] parts = input.Split(' ');
string lastWord = parts[parts.Length - 1];
While this would work for this string, it might not work for a slightly different string, so either you'll have to figure out how to change the code accordingly, or post all the rules.
string input = ".... ,API";
here, the comma would be part of the "word".
Also, if the first method of obtaining the word is correct, ie. everything after the last space, and your string adheres to the following rules:
Will always contain at least one space
Does not end with one or more space (in case of this you can trim it)
then you can use this code that will allocate fewer objects on the heap for GC to worry about later:
string lastWord = input.Substring(input.LastIndexOf(' ') + 1);
I hope its help

Categories