Replace Values in a Uri Query String

Replace item in querystring

Maybe you could use the System.UriBuilder class. It has a Query property.

replace url query string value

Javascript Strings are immutable. So once their value got assigned they can't changes their values.

So you have to reassign them with new changes or have to receive them in new variables. Generally we reassign as there is no need of new variable to be introduced unless you need old value .

  postURL = postURL.replace('__param__', mobileNo);

replace url query string values using javascript

Maybe you're looking for something like this:

var url = new URL('https://example.com?src=a&dest=b');
var src = url.searchParams.get('src');var dest = url.searchParams.get('dest');
console.log('current src:', src);console.log('current dest:', dest);
var search_params = new URLSearchParams(url.search);
search_params.set('src', 'c');search_params.set('dest', 'd');
url.search = search_params.toString();
var new_url = url.toString();
console.log(new_url);

better way to replace query string value in a given url

How about something like this?

function merge_querystring($url = null,$query = null,$recursive = false)
{
// $url = 'http://www.google.com.au?q=apple&type=keyword';
// $query = '?q=banana';
// if there's a URL missing or no query string, return
if($url == null)
return false;
if($query == null)
return $url;
// split the url into it's components
$url_components = parse_url($url);
// if we have the query string but no query on the original url
// just return the URL + query string
if(empty($url_components['query']))
return $url.'?'.ltrim($query,'?');
// turn the url's query string into an array
parse_str($url_components['query'],$original_query_string);
// turn the query string into an array
parse_str(parse_url($query,PHP_URL_QUERY),$merged_query_string);
// merge the query string
if($recursive == true)
$merged_result = array_merge_recursive($original_query_string,$merged_query_string);
else
$merged_result = array_merge($original_query_string,$merged_query_string);
// Find the original query string in the URL and replace it with the new one
return str_replace($url_components['query'],http_build_query($merged_result),$url);
}

usage...

<a href="<?=merge_querystring($url,'?page=1');?>">Page 1</a>
<a href="<?=merge_querystring($url,'?page=2');?>">Page 2</a>

How to replace query parameter only if it exists using string replace All regex

First let's go through what your regex is doing, then we can fix it.
Your regex:

^ - the beginning of the string
(.*) - match any character 0 or more times - as many times as possible (greedy)
[?]? - match `?` 0 or 1 times
(.*) - match any character 0 or more times - as many times as possible (greedy)
$ - the end of the string

Really the main problem here is that the first capturing group captures as many times as possible, so that'll always match the entire url. We can make that non-greedy by using .*?, so we end up with ^(.*?)[?]?(.*)$. However now we end up with the problem that the last capturing group captures the entire url - we could make this non-greedy but then it wouldn't match any characters at all. Instead, we should make sure that this group will only capture when ? is present, so we can make [?]? non-optional, move it into the next capturing group, and make the last group optional, like this: ([?](.*))?. Whilst we're at it, we might as well use \? instead of [?] and we end up with ^(.*?)(\?(.*))?$. This works, as the $ signifies that we want to capture right up to the end. With this we'd need to use $3 instead of $2 as $2 now contains ? as well when replacing, so we can use a non-capturing group to eliminate that problem.
So our final regex is /(.*?)(?:\?(.*))?/g.

Your final code will look like this:

let url = "https://test1.com/path?query1=value1"
console.log(url.replaceAll(/^(.*?)(?:\?(.*))?$/g,"$1?newquery=newvalue&$2"))
url = "https://test1.com/path"
console.log(url.replaceAll(/^(.*?)(?:\?(.*))?$/g,"$1?newquery=newvalue&$2"))

How to I replace a part of the URL string containing a query using htaccess?

With your shown samples, please try following htaccess rules.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##External redirect for url change in browser rules here...
RewriteCond %{THE_REQUEST} \s/(pages)/map/?\?name=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]

##Internal rewrite here for internal file's serving here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1/all-maps.php?name=$2 [QSA,L]

NOTE: I have mentioned map? in rewrite rules(2nd set of rules written in comments also in rules), you can change it with your php file's name whatever php file you have to pass query string to.

NOTE2(OP's fixes as per OP environment): tweaked 1 rule a
bit to $1/all-maps.php?name=$2 to $1/map.php?name=$2 and moved the all-maps.php one directory down and these rules worked fine, mentioned by OP in comments here. Just sharing here, in future it could help people that apart from above rules this was done as part of solution.



Related Topics



Leave a reply



Submit