Delete all text up to a character [duplicate] - javascript

This question already has answers here:
RegEx - Get All Characters After Last Slash in URL
(8 answers)
Closed 2 years ago.
I currently have a url path like so:
var str = "http://stackoverflow.com/i-like-upvotes/you-do-too";
I wish to delete/remove all the string up to the last / slash.
If I wanted to replace the last slash I'd get it like this:
console.log(str.replace(/\/(?=[^\/]*$)/, '#'));
But I would like to delete everything up to the last slash and I can't figure it out.
In the above example I'd get back
you-do-too
Any ideas?

var str = "http://stackoverflow.com/i-like-upvotes/you-do-too";
console.log(str.replace(/^.*\//, ''));
^ matches the beginning of the string.
.* matches anything.
\/ matches slash.
Since .* is greedy, this will match everything up to the last slash. We delete it by replacing with an empty string.

I know it's not beautiful, but you can do str.split('/').pop() to get the last part of the URL.
No un-humanly readable regex

Related

How to replace before first forward slash [duplicate]

This question already has answers here:
Regex: match first slash in url -- thats it!! ? not working for some reason?
(2 answers)
Closed 2 years ago.
I have this string for example:
"prop/items/option-1/value"
I want to replace before first forward slash. Expected output would be:
"newvalue/items/option-1/value
I could not find suitable Regex to replace this pattern, Can you please help?
let regex = /^[^/]+/
let string = "prop/items/option-1/value"
console.log(string.replace(regex, 'newvalue'))
Here's the regex you need.
^ - match from start of string
[^/]+ - match one or more characters except for forward slash.
Another option is to use substr
const str = "prop/items/option-1/value";
// Use substr
const newStr = 'newvalue' + str.substr(str.indexOf('/'));
// Log
console.log(newStr);

How to capture only first occurence in this regex? [duplicate]

This question already has answers here:
What is the meaning of the 'g' flag in regular expressions?
(10 answers)
Closed 4 years ago.
I have this string:
2+*+2+*+3+*+3+*
And this regex:
(\d{1,3}\+\*\+\d{1,3})
Problem is, it captures both matches (2++2 and 3++3), and I need it to capture only the first occurence.
How can I change my regex to match this?
Here's the regex101 url for anyone who wants to try:
https://regex101.com/r/7G2adU/1/
Thanks!!
If you change your regex to:
^(\d{1,3}\+\*\+\d{1,3})
then only the first match will be captured. The caret ^ is a start-of-string anchor; it matches the start of the input string so that only the first occurrence of the rest of the regex is captured.
There is another alternative. In your example https://regex101.com/r/7G2adU/1/ your regex looks like this:
/(\d{1,3}\+\*\+\d{1,3})/ig
The g is the global flag; it tells the engine not to stop after the first match has been found, but rather to continue until no more matches can be found. If you remove that flag, only the first match will be returned.

Regex allows spaces [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
For the following regex expression:
var regex = new RegExp("^(www\\.)?[0-9A-Za-z-\\.#:%_\+~#=]+(\\.[a-zA-Z]{2,})+(/.*)?(\\?.*)?");
I don't understand why the string "www.goo gle.com" passes the regex test. When I did this:
var regex = new RegExp("^(www\\.)?[0-9A-Za-z-\\.#:%_\+~#=]+(\\.[a-zA-Z]{2,})+(/.*)?(\\?.*)?$");
i.e. adding $ in the end of the regex string prevents the above string passing, which is what I would want.
I tried finding a "simulator" online to help me figure out how the regex is matching but couldn't find much help.
www.goo gle.com passes the test since, www. is matched by [0-9A-Za-z-\\.#:%_\+~#=]+ and
goo is matched by (\.[a-zA-Z]{2,})+. In contrast, (www\\.)?, and the last two groups are optional, so the regex is satisfied even if they are not matched, hence there's no need to further match gle.com.
By adding $, the regex no longer matches, since the space is not matched by any of the subexpressions.

regex extract last occurrence ignoring new line [duplicate]

This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Closed 7 years ago.
Considering a string like:
"#foo #foo#foo#foo #foo\n
foofoofoo\n
foo #bar"
I try for 2 days to extract the last #/# occurrence so here, the # before 'bar'. For now, i have something like this [##](?!.*[##]) which seems to work except when user insert new lines in there.
Can someone give me some tips please?
You can use this lookhahead regex:
/[##](?![\s\S]*[##])/
RegEx Demo
(?![\s\S]*[##]) is the negative lookahead that asserts there is no # or # ahead of current position in any line. [\s\S] matches any character including newline.

In Javascript how can I check if a string contains \ (backslash) at the end of it, and if so remove it? [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
Regex to remove last / if it exists as the last character in the string
Javascript: How to remove characters from end of string?
In Javascript how can I check if a string contains \ (backslash) at the end of it, and if so remove it? Looking for a regex solution.
Appreciate your time and help.
if (myString.match(/\\$/)) {
myString = myString.substring(0, myString.length - 1);
}
The Regex '\$' Will match an escaped character '\' followed by the end of line. If this is a match, it runs the substring method to get everything but the last character.
As pointed out, in this case this can be simplified to:
myString = myString.replace(/\\$/, "");
(Thankyou #Lekensteyn for pointing it out)
I've left both answers up so one can see the methodology if removing the match is no longer the goal.
I'd suggest:
var string = 'abcd\\';
if (string.charAt(string.length - 1) == '\\'){
// the \ is the last character, remove it
string = string.substring(0, string.length - 1);
}
References:
charAt().

Categories