I had following string ,
const str= "this is harp//foo.eps and harp//data.eps"
I need to replace the string between harp to .eps
My code below,
const str = "this is harp//foo.eps and harp//data.eps"
const data = str.replace(/\harp.*\eps/, 'www.imge.jpg');
console.log(data);
it was returning output below,
this is www.imge.jpg
But expected output
this is www.imge.jpg and www.imge.jpg
Try to make use non-greedy (?) search and global (/g) modifier:
const str = "this is a harp//foo.eps and a harp//data.eps and harp//data.eps"
const data = str.replace(/harp(.*?)eps/g, 'www.imge.jpg');
console.log(data);
You can do it using the replace all function and the following regex
const str = "this is harp//foo-two.eps and harp//data-9.eps"
const newSTr = str.replaceAll(/harp\/\/[A-Za-z\-0-9]+\.eps/g, 'www.imge.jpg')
console.log(newSTr);
Related
If I do regex match for
str = "/mypage/account/info.jsp"
str.match('\/.*\.jsp')
I get entire string but I want to grab only "info"
How can I accomplish using only regex?
First, you can get the text after the last /
/[^/]*$/
and then get the desired result using split
const str = "/mypage/account/info.jsp";
const match = str.match(/[^/]*$/);
const result = match && match[0].split(".")[0];
console.log(result);
only regex
const str = "/mypage/account/info.jsp";
const match = str.match(/[^/]+(?=\.jsp$)/);
console.log(match[0]);
Given the following string:
const myString = "This is my comment content. [~firstName.lastName]";
What is a javascript regex to extract the "firstName" and "lastName"?
What is a javascript regex to extract the content minus the "[~firstName.lastName]"?
I'm rubbish with Regex.
const myString = "This is my comment content. [~firstName.lastName]";
const first = myString.match(/.*\~(.*)\./)[1]
const last = myString.match(/.*\.(.*)]/)[1]
const both = myString.match(/.*\~(.*)\]/)[1]
console.log(first)
console.log(last)
console.log(both)
Here's a good website for regex help
You can try this-
let myString = "This is my comment content. [~firstName.lastName] and the original name is [~John.Doe]";
const regex = /\[~([^\]]+)\.([^\]]+)\]/g;
let match = regex.exec(myString);
const names = [];
while(match !== null) {
names.push({firstName: match[1], lastName: match[2]});
match = regex.exec(myString);
}
// Remove the pattern from the original string.
myString = myString.replace(regex, '');
console.log(names, myString);
This code will find all the matches found for the pattern.
You could target each name separately. match will return the complete match as the first element of the array, and the groupings specified by the ( and ) in the regex as the subsequent elements.
const str = 'This is my comment content. [~firstName.lastName]';
const regex = /\[~(.+)\.(.+)\]/;
console.log(str.match(regex));
Or you could target just the characters within the brackets and then split on the ..
const str = 'This is my comment content. [~firstName.lastName]';
const regex = /\[~(.+)\]/;
console.log(str.match(regex)[1].split('.'));
I have a string like this:
const myString = "["dupa", "dupa", "dupa"]";
how to use regex in JS/TS to remove all characters " and [ and ]?
i need only dupa, dupa, dupa
thanks for any help
It will return you string with other removed from it and with your double quoted there is problem with it. So use single quote.
const myString = '["dupa", "dupa", "dupa"]';
const a = myString.replace(/[\[\]']+/g, '').replace(/"/g, '');
console.log(a);
You will get "dupa, dupa, dupa"
Or you can go with :
const myString = '["dupa", "dupa", "dupa"]';
const a = JSON.parse(myString);
console.log(a.join(", "))
I have the following string:
let str = '/user/:username/'
I want to extract replace username with harry with colon removed.
I tried the following:
const regex = /[^:]+(?=:)/g
str.replace(regex, x => console.log(x))
Try: /:\w+/
let str = '/user/:username/'
str.replace(/:\w+/, "harry")
// => "/user/harry/"
let str = '/user/:username/';
let html = str.replace(":username", "harry");
console.log(html);
var str = '/user/:username/';
var newstr = str.replace(/:username/i, "harry");
print(newstr);
hi pal, is this what you are looking for? i found it at https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/replace
you can use like that :
let str = '/user/:username/';
str.replace(':username','harry')
// o/p => /user/harry/"
In your regex [^:]+(?=:) you are matching 1+ times not a colon and assert that at the end there should be a colon resulting in a match for /user/
If you want to use the negated character class you could match a colon and then not a forward slash:
:[^\/]+
const str = `/user/:username/`;
const result = str.replace(/:[^\/]+/, "harry");
console.log(result);
I have a string look like:
var str = https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none
I want to remove at start ?pid= to end. The result look like:
var str = https://sharengay.com/movie13.m3u8
I tried to:
str = str.replace(/^(?:?pid=)+/g, "");
But it show error like:
Invalid regular expression: /^(?:?pid=)+/: Nothing to repeat
If you really want to do this at the string level with regex, it's simply replacing /\?pid=.*$/ with "":
str = str.replace(/\?pid=.*$/, "");
That matches ?pid= and everything that follows it (.*) through the end of the string ($).
Live Example:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.replace(/\?pid=.*$/, "");
console.log(str);
You can use split
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none"
var result = str.split("?pid=")[0];
console.log(result);
You can simply use split(), which i think is simple and easy.
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.split("?pid");
console.log(str[0]);
You may create a URL object and concatenate the origin and the pathname:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
var url = new URL(str);
console.log(url.origin + url.pathname);
You have to escape the ? and if you want to remove everything from that point you also need a .+:
str = str.replace(/\?pid=.+$/, "")
You can use split function to get only url without query string.
Here is the example.
var str = 'https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none';
var data = str.split("?");
alert(data[0]);