why this regular expression replacement doesnt work?
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace('/page\/[0-9]+/', 'page/2'); //it must become http://myweb.com/page/2/id/2
You need to do two things:
change str.replace to url.replace
remove the ' around the regex
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace(/page\/[0-9]+/, 'page/2');
Example: http://jsfiddle.net/fhqXn/
Use url rather than str, if you want to replace something in the String stored in the url variable.
You have a naming misspell.
Rename your url var to str or change the str.replace to url.replace:
newUrl = url.replace('/page\/[0-9]+/', 'page/2');
Related
I am totally new to regex. I need to grab particular set of string in href link dynamically (in this case is 1.867172) that I need to test.
<href = 'http://test.stage.cms.9c9media.net:8080/pete-test-1.867172'>
I don't think regex is necessary. A simple split could achieve this.
let URL = "http://test.stage.cms.9c9media.net:8080/pete-test-1.867172"
let testID = URL.split('-')[2]
console.log(testID)
Assuming that the number you want is always between a dash and the closing '
https://regexr.com/48s6u
If you want a regex solution. Please note this will capture any part of string of format <number>.<number>
var source = "http://test.stage.cms.9c9media.net:8080/pete-test-1.867172"
var regex = /\d\.\d+/;
var matches = source.match(regex);
if(matches)
console.log(matches[0])
Maybe you can use the next regular expression with a capturing group for all the characters following the last - char:
regular expression: /.*-(.*)/
Combine the previous regular expression with String.match to get what you looking for:
let link = document.getElementById("myLink");
let url = link.attributes.href.value;
let pattern = url.match(/.*-(.*)/)[1];
console.log("pattern: " + pattern);
<a id="myLink" href='http://test.stage.cms.9c9media.net:8080/pete-test-1.867172'></a>
I have a folder path that always starts with a certain string which I want to remove. Let's say it looks like this:
my-bucket/2929023/32822323/file.jpg
I want it to look like this:
2929023/32822323/file.jpg
How would I do that? Thanks!
Using the functions substring and indexOf from String.prototype.
var str = "my-bucket/2929023/32822323/file.jpg";
console.log(str.substring(str.indexOf('/') + 1))
You could use a simple replace method if the string is only present once;
var string = "my-bucket/2929023/32822323/file.jpg";
var revisedString = string.replace('my-bucket/', '');
console.log(revisedString);
However, you're also able to use a Regex (regular expression) to remove it as well, something like;
var string = "my-bucket/2929023/32822323/file.jpg";
console.log(string.replace(/^my-bucket\//, ''));
Use a regex to rip the first one out. No substrings necessary.
var myString= "my-bucket/2929023/32822323/file.jpg";
myString = myString.replace(/^.+?[/]/, '');
I want to know way to replace substring in url with new string.
https://pbs.twimg.com/media/DW7hPt9VAAAdKE7?format=jpg&name=small
after "&name=" they are many kind of size like
900x900,medium,360x360,small
let href = document.location.href;
if(!href.includes('&name=orig')){
if(href.includes(/900x900|medium|360x360|small/){ //if href have some size in regular expression
// I try to make it search for substring in regular expression
document.location.href = href.replace(/900x900|medium|360x360|small/,'orig');
}
else{ //if url don't have '&name=' like above
var adding = '&name=orig';
document.location.href = link+adding;
}
}
It not working
I don't want to write code to check all case like
if(href.includes('900x900')
if(href.includes('medium')
if(href.includes('360x360')
if(href.includes('small')
they are way to find match at once?
change if(href.includes(/900x900|medium|360x360|small/){ to
if(href.search(/900x900|medium|360x360|small/) !== -1){
as includes accepts the string not the regex.
I need to remove size i.e. resolution of image path from url. Take following example:
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg
I need to remove -1024x682
http://raudevlocal.com/wp/wp-content/uploads/2017/05/rohit-300x118.jpg
Need to remove -300x118
use regex pattern
$(document).ready(function(){
window.location.href = window.location.href.replace(/(-\d{2-6}x\d{2-6})/g,'');
});
str = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
str.replace(/-(\d+x\d+)/, "");
output will be
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake.jpeg
use regular expression
var string = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
console.log(string.replace(/-\d+x\d+/g,''));
window.loaction.href= window.location.href.replace(/-\d+x\d+/g,'');
You can use replace function
window.location.href.replace('-1024x682', '');
If you want to use the new one as your current url then try
window.location.href = window.location.href.replace('-1024x682', '');
I have a URL e.g. http://localhost:8000/#Test/Method
I am doing following:
System.Windows.Browser.HtmlPage.Window.CurrentBookmark = string.Empty;
which just remove Test/Method but not '#'
I need to modify browser URL
as:
http://localhost:8000/
Any ideas on how to fix this?
If you are manipulating a String you can use the following method :
String Url = "Foo#Bar";
Url = Url.Replace("#", string.Empty);
Use regular expression
String url = "http://localhost:8000/#Test/Method"
url = url.replace(/#/g, "");
The above reg expression will scan for all occurrences of '#' character and replaces with empty character