How to replace before first forward slash [duplicate] - javascript

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

Related

Why isn't this simple alphanumeric regex matching with JS but it is matching on regex101? [duplicate]

This question already has an answer here:
Why this javascript regex doesn't work?
(1 answer)
Closed 1 year ago.
I'm trying to test the string UNCLTEST614NESZZ.
Using the regex /^[a-z0-9]+$/i.
Below is the code being used.
let regex = new RegExp("/^[a-z0-9]+$/i");
let match = regex.test(serial);
Yet match ends up being false, despite the same regex and test string in regex101 producing a positive result
https://regex101.com/r/MrpcB5/1
Based on the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
You can either use
let regex = new RegExp("^[a-z0-9]+$", "i");
or
let regex = /^[a-z0-9]+$/i;

Delete all text up to a character [duplicate]

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

javascript regex find words contains (at) in text [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I've got a bunch of strings to browse and find there all words which contains "(at)" characters and then gather them in the array.
Sometimes is a replacement of "#" sign. So let's say my goal would be to find something like this: "account(at)example.com".
I tried this code:
let gathering = myString.match(/(^|\.\s+)((at)[^.]*\.)/g;);
but id does not work. How can I do it?
I found a regex for finding email addresses in text:
/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)
I think about something similar but unfortunately I can't just replace # with (at) here.
var longString = "abc(at).com xyzat.com";
var regex = RegExp("[(]at[)]");
var wordList = longString.split(" ").filter((elem, index)=>{
return regex.test(elem);
})
This way you will get all the word in an array that contain "at" in the provided string.
You could use \S+ to match not a whitespace character one or more times and escape the \( and \):
\S+\(at\)\S+\.\w{2,}

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().

How do I replace all string values in this JavaScript statement? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace all occurrences in a string
I have this string:
"12-3-02"
And I would like to convert it to :
"12/3/02"
How would I do this? I 've tried :
.replace(/-/,"/")
But this only replaces the first one it finds. How do I replace all instances?
One of these (using split, or a global RegEx flag):
str = str.split('-').join('/'); // No RegExp needed
str = str.replace(/-/g, '/'); // `g` (global) has to be added.
Add the g (global) modifier to the regex, to match all -s.
.replace(/-/g,"/")
If you want to replace all the occurences of - by / then use this where g specifies the global modifier.
"12-3-02".replace(/-/g,"/");
Working demo - http://jsfiddle.net/ShankarSangoli/QvbM8/
Try with:
"12-3-02".replace(/-/g, '/');
This question has been posed about a thousand times, but no one has ever considered the possibility that, in the real world, characters can occur in places where you might not expect.(Typo's in input or replacing parts of words when you only wanted to replace a single word...
var reformat = '01-11-2012'.replace(/(([0-9]+)(?=\-))\-(?=[0-9])/g,'$1/');
This will only replace '-' characters that are preceded and followed by a number.

Categories