How do I change this value using regex? - javascript

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

Related

Javascript: add a character to a string containing a specific word

If I have the following string: table.row.columns.values.many. I am looking to add character * after values.
So expected output would be: table.row.columns.values*.many .
If there is no "values" in string then string should stay the same.
Please use this code.
let str = "table.row.columns.values.many"
str = str.replace("values", "values*");
console.log(str);
You can use also "replaceAll" if you want to replace all "values".
I think this works nice for you, really simple, and easy way
var str = "table.row.columns.values.many";
str = str.replace("values", "values*")
console.log(str)

Replace a URL parameter using JQuery 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]+/;

javascript split() of a attribute src of an iframe?

I have this:
<iframe src="http://999.999.99.99/reporting/report.php?rid=2&gid=6&ftid=0&fid=1000&gty=1&ltid=1&lid=1000&mode=7&width=220&sid=0&cc=666666,ffa401">
I tried split() in getting the value between rid= and & but it does not get what I wanted. I only wanted the value between those two. Please help.
This is my code so far:
var source = 'http://999.999.99.99/reporting/report.php?rid=2&gid=6&ftid=0&fid=1000&gty=1&ltid=1&lid=1000&mode=7&width=220&sid=0&cc=666666,ffa401';
var rid = source.split('rid= &').pop();
alert(rid);
Using a URL parser described here.
var parts = getUrlParts(url);
var rid = parts.rid;
Use a regular expression
var str="http://999.999.99.99/reporting/report.php?rid=2&gid=6&ftid=0&fid=1000&gty=1&ltid=1&lid=1000&mode=7&width=220&sid=0&cc=666666,ffa401";
var re = /[?&]rid=([^&]+)/i;
var part = str.match(re);
console.log(part[1]);
Consider using a simple regular expression:
var rid = src.match(/rid=(\d+)/)[1];
You could match against a regular expression, but if you want to do that with split, try:
var a = src.split("rid=")[1].split("&")[0];
You can use this code:
var rid = src.substr(src.indexOf('rid') + 4).split('&')[0];
Thanks a lot guys, I can really count on you guys here in SO.
So far, from your answers, I have used match() and it worked fine. Really, thank you guys. :)

javascript replace question mark

how to make a regex for ? and = in javascript?
I want something from
http://localhost/search?search=words
to
http://localhost/search/search/words
(?search=) to (/search/)
<script>
var ss = "http://localhost/search?search=words".replace("/\?search\=/g", "/search/");
document.write(ss);
</script>
BTW: just some prastic, not a htaccss rewrite. Thanks.
Almost there! = is not a special character and does not need to be escaped. In addition, regex strings are not wrapped by quotes. So:
"http://localhost/search?search=words".replace(/\?search=/g, "/search/");
How about
str.replace(/[?=]/g, "/");
Do note that it's probably better to make a function to understand the url structure and rebuild it properly, that will produce a much more healthy, robust code, rather then a simple replacement.
You can use a simple string for replace:
var ss = "http://localhost/search?search=words".replace("?search=", "/search/");

Simple Javascript string manipulation

I have a string that will look something like this:
I'm sorry the code "codehere" is not valid
I need to get the value inside the quotes inside the string. So essentially I need to get the codehere and store it in a variable.
After some researching it looks like I could loop through the string and use .charAt(i) to find the quotes and then pull the string out one character at a time in between the quotes.
However I feel there has to be a simpler solution for this out there. Any input would be appreciated. Thanks!
You could use indexOf and lastIndexOf to get the position of the quotes:
var openQuote = myString.indexOf('"'),
closeQuote = myString.lastIndexOf('"');
Then you can validate they are not the same position, and use substring to retrieve the code:
var code = myString.substring(openQuote, closeQuote + 1);
Regex:
var a = "I'm sorry the code \"codehere\" is not valid";
var m = a.match(/"[^"]*"/ig);
alert(m[0]);
Try this:
var str = "I'm sorry the code \"cod\"eh\"ere\" is not valid";
alert(str.replace(/^[^"]*"(.*)".*$/g, "$1"));
You could use Javascript's match function. It takes as parameter, a regular expression. Eg:
/\".*\"/
Use regular expressions! You can find a match using a simple regular expressions like /"(.+)"/ with the Javascript RegExp() object. Fore more info see w3schools.com.
Try this:
var msg = "I'm sorry the code \"codehere\" is not valid";
var matchedContent = msg.match(/\".*\"/ig);
//matchedContent is an array
alert(matchedContent[0]);
You should use a Regular Expression. This is a text pattern matcher that is built into the javascript language. Regular expressions look like this: /thing to match/flags* for example, /"(.*)"/, which matches everything between a set of quotes.
Beware, regular expressions are limited -- they can't match nested things, so if the value inside quotes contains quotes itself, you'll end up with a big ugly mess.
*: or new RegExp(...), but use the literal syntax; it's better.
You could always use the .split() string function:
var mystring = 'I\'m sorry the code "codehere" is not valid' ;
var tokens = [] ;
var strsplit = mystring.split('\"') ;
for(var i=0;i<strsplit.length;i++) {
if((i % 2)==0) continue; // Ignore strings outside the quotes
tokens.push(strsplit[i]) ; // Store strings inside quotes.
}
// Output:
// tokens[0] = 'codehere' ;

Categories

Resources