Replace a URL parameter using JQuery Javascript - javascript

i have a url that has several parameters, and i want to replace them using JQuery (not javascript).
var str = "http://example.com/index.php?color=red&size=large&quantity=5";
var newQty = 10;
How can i change the quantity using JQuery ONLY if quantity exists as a parameter? How can i change a parameter that isn't at the end of the query string, such as size=large?
I prefer to use regex and .replace function, if anyone can help me with the proper code please... something like this.
var newStr = str.replace(REGEX, "quantity="+newQty);

var REGEX = /quantity\=[0-9]+/;

Related

Change a unique Character on jQuery

<span class="number1">10.00</span>
simply, i want to replace the '.'(dot) for a ','(comma) using jQuery.
I've tried several forms to search the $('.number1') characters and replacing
it with a comma.
<span class="number1">10.00</span>
What if there is more than one Dot in the string?
Why use jQuery for such a simple operation? What you need is a simple string manipulation. Adding a library so that you can type a few less characters to do something so basic seems hardly worth it.
What you really need the the plain old JavaScript String.replace() method.
Here's jQuery and non-jQuery ways to do it:
// With jQuery:
console.log($(".number1").text().replace(".", ","));
// Without jQuery:
console.log(document.querySelector(".number1").textContent.replace(".", ","));
// When you need to replace all the . chars. in the string, you'll need to use
// a regular expression with .replace().
// The / / denote the delimiters of a regular expression
// The \. is the escape code for a .
// The g means do a global find/replace throughout the string
// With jQuery:
console.log($(".number1").text().replace(/\./g, ","));
// Without jQuery:
console.log(document.querySelector(".number1").textContent.replace(/\./g, ","));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="number1">10.00.00</span>
Try this answer.
$(".number1").text(function () {
return $(this).text().replace(/\./g, ",");
});
this is a solution in vanilla js:
let spanNumber = document.querySelector('.number1')
let number = spanNumber.textContent
let newNumber = number.split('.').join(',')
spanNumber.innerHTML = newNumber
short version with replace:
let DOMElement = document.querySelector('.number1')
let string = DOMElement.textContent.replace('.',',')
DOMElement.innerHTML = string
Just use built in DOM property innerHTML, instead of unecessary jQuery mambo jambo like:
var num1 = document.querySelector('.number1');
num1.innerHTML = num1.innerHTML.replace('.', ',');
InnerHTML is value between your HTML tags, and since its a string it has access to all String prototype methods and properties.
Just turn it to array by finding a dot delimiter with split method, then get it right back using join like:
// Lets say that the value is 10.05.53.324.343
var num1 = document.querySelector('.number1');
num1.innerHTML = num1.innerHTML.split('.').join(','); // Outputs 10,05,53,324,343
You could use the Intl.NumberFormat object instead of replacing characters:
Perhaps you have other numbers on your content that you want to format.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
But you can check a working example below that formats a number on US-EN format to pt-BR:
var el = document.querySelector('.number1');
var value = parseFloat(el.textContent);
var newValue = new Intl.NumberFormat('pt-BR', { minimumFractionDigits: 2 }).format(value);
el.innerHTML = newValue;
<span class="number1">10.00</span>

How do I change this value using regex?

I have a string that's structured as so:
"http://mydomain.com/?i=0"
I'd like to be able to change the value of i using a regular expression in javascript but I'm not sure how to do it. Is anyone able to help?
Here's a place to start:
str = "http://mydomain.com/?i=0";
str = str.replace(/((?:\?|&)i=)([^&]*)/g, '$1foo');
"http://mydomain.com/?i=0".replace(/i=.*$/, 'i='+<newValue>);
if you need to cater for additional parameters following i use
"http://mydomain.com/?i=0&j=k".replace(/i=[^&]+/, 'i='+<newValue>);
there is a fiddle to demonstrate it
Use the javascript replace() function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
I'm not going to do the Regex for you as you have demonstrated no attempt at doing it yourself.
Example:
var str = "http://mydomain.com/?i=0";
var i=1; // or any other value (e.g. i="test")
str = str.replace(/i=.*/, 'i='+i);
alert(str);

Get portion of a string using Javascript

I have a string (from the pathname in the url) and I am trying to pull out part of it, but I'm having trouble.
This is what I have so far:
^(/svc_2/pub/(.*?).php)
The string is:
/svc_2/pub/stats/dashboard.php?ajax=1
How can I get a regex that returns /pub/stats/dashboard only?
If it's always that format (I'm assuming the /svc_2/ is always there) this should do it.
var s = "/svc_2/pub/stats/dashboard.php?ajax=1";
var match = s.match(/\/svc_2(.+)\./)[1];
But not if anything comes before that.
For this, using a regex is too much, but here is:
var string = "/svc_2/pub/stats/dashboard.php?ajax=1";
console.log(string.replace(/.*(\/pub.*)\.php.*$/,"$1"));
But you can do it, without a regex, like this
console.log(string.substr(6,string.indexOf(".php")-6));
In both cases, the console.log will give you /pub/stats/dashboard
This is not very flexible, but should work in this specific case.
var basedir = '/svc_2/', str = '/svc_2/pub/stats/dashboard.php?ajax=1';
str = str.substring(basedir.length, str.indexOf('.'));
alert(str);

remove last two parameters from URL

i want to break a following url
http://www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434&cn=bella&cjid=3344324
into this by eliminating last two parametes i.e. &cn=bella&cjid=3344324
http://www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434
the length of the url may change but the last two parameters remains in that position only. so how can i remove that in a efficient way.
A RegExp is the easiest way for this case:
str = str.replace(/&[^&]*&[^&]*$/,'');
You can use replace with regular expression. If the url is in var url then you can use this one
var new_url = url.replace(/&cn=.*/, '');
you can test it with
var url = 'http:\www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434&cn=bella&cjid=3344324';
console.info(url.replace(/&cn=.*/, ''));
var string = "http://dom.com/?one=1&two=2&three=3&four=4";
string.match(/(.*)&(.*)&(.*)/)[1]; // strips last two parameters
You can use regular expressions to replace the last 2 parameters with the empty string:
var url = "http://www.example.com/?p1=1&p2=2&p3=3&p4=4";
var urlWithoutLast2Parameters = url.replace(/&[^&]+&[^&]+$/,"");
You could use the function IndexOf to find the location of the '&cn' and then just use the substring function to create a new string eliminating the '&cn' portion of the URL, so something like...
var intIndexOf = str.IndexOf('&cn=')
strURL = strURL.substring(0,intCharAt)

Parse string in javascript

How can I parse this string on a javascript,
var string = "http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1";
I just want to get the "265956512115091" on the string. I somehow parse this string but, still not enough to get what I wanted.
my code:
var newstring = string.match(/set=[^ ]+/)[0];
returns:
a.265956512115091.68575.100001022542275&type=1
try this :
var g=string.match(/set=[a-z]\.([^.]+)/);
g[1] will have the value
http://jsbin.com/anuhog/edit#source
You could use split() to modify your code like this:
var newstring = string.match(/set=[^ ]+/)[0].split(".")[1];
For a more generic approach to parsing query strings see:
Parse query string in JavaScript
Using the example illustrated there, you would do the following:
var newstring = getQueryVariable("set").split(".")[1];
You can use capturing group in the regex.
const str = 'http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1';
console.log(/&set=(.*)&/.exec(str)[1]);

Categories

Resources