This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
add or update query string parameter
I am trying to replace the page number in the query string no matter what digit is to 1.
query string
index.php?list&page=2&sort=epub
javascript
window.location.href.replace(new RegExp("/page=.*?&/"), "page=1&")
Your code looks almost right; however:
you need to use either new RegExp or the special // regex syntax, but not both.
the replace method doesn't modify the string in-place, it merely returns a modified copy.
rather than .*?, I think it makes more sense to write \d+; more-precise regexes are generally less likely to go awry in cases you haven't thought of.
So, putting it together:
window.location.href = window.location.href.replace(/page=\d+/, "page=1");
Related
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 2 years ago.
Please note that this is not a json object :) My string is:
{"message":"***error in SAP module:-1***","status":400}
This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.
So, I want to take only the bold string (all the value of "message").
I thought about lastIndexOf, and pick the string between ":" and ","
But I got messed up with the escape characters for the quotes.
How can I achieve it with lastIndexOf? Or in another better way?
If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "
This question already has answers here:
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 4 years ago.
My title might not be worded correctly sorry in advance.
I've looked everywhere and I honestly can't seem to figure it out.
I want to use a variable in a RegEx but the solution that i've found and tried to work off of works but it is not flexible for what I need. I can't seem to figure out how to convert it to a RegEx constructor object which I think is my best bet. Any help will be greatly appreciated.
let str = 'haeandviaecy'
let newString = str.replace(/(\w{3})/g, '$1 ').replace(/(^\s+|\s+$)/, '')
//hae and via ecy
replace(/(^\s+|\s+$)/,'') removes any preceding or trailing space from the string - just giving you a heads up
So what this snippet does is go to every third and put a blank space in it. What I want is to make my code more flexible and be able to put a variable to let me choose the number for when it puts a blank space using a variable.
right now it prints out - "hae and via ecy"
for ex. every 4th would be - haea ndvi aecy
I've read you cant put in variables unless it is a RegEx contructor object. When I've tried to convert it doesn't seem to work.
If gather the question correctly, you can use a template literal as parameter passed to RegExp constructor.
This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 4 years ago.
I have a regex when I instantiate the Regex object like this:
this.checkRegex = new RegExp(/^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$/);
It works fine, however If I store the regex in string it does not work:
private checkReg: string = '/^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$/';
this.checkRegex = new RegExp(this.checkReg);
I am using angular-typescript. What is the thing I am missing here when I am trying to instantiate by throwing string inside the constructor. Code sample will be really appreciated. Thanks for your help.
When you are passing a string to the RegExp constructor, you need to change it a little bit. Instead of
'/^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$/'
You would omit the preceding and trailing slash
'^([0|\\[+][0-9]{0,5})?([1-9][0-9]{0,15})$'
Note also double escaping the back slash.
If you want to store the RegExp as a String, store it without the forward slashes at the front and back. When creating the RegExp object, those get escaped:
new RegExp('/^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$/');
will result in
/\/^([0|[+][0-9]{0,5})?([1-9][0-9]{0,15})$\//
while,
new RegExp('^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$');
will work:
/^([0|[+][0-9]{0,5})?([1-9][0-9]{0,15})$/
This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 6 years ago.
I am communicating to a REST server which is expecting something like http://example.com/1.0/editor/5/bla/26 along with body parameters of x, y, and z. Or maybe instead of /1.0/editor/5/bla/26, it should be 1.0/editor/5/bla/what about item #26, but of course with what about item #26 escaped.
There are many post describing how to use jQuery.param() to encode parameters, however, that is not my question. How chould the actual url be created using jQuery of JavaScript?
$.ajax({
type:'PUT',
url:'/1.0/editor/'+$('#id').val()+'/bla/'+$('#wha').data('id'),
data:{x:x,y:y,z:z},
success: function (error){}
});
Can you write a javascript RegEx function to replace all spaces with a known character? Or replace it with its HTML equivalent. Then in your backend, just look for that escape character and replace them with the spaces again. It's kind of a work around but depending on your situation it could work.
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
RegExp gurus, heed my call!
This is probably super simple, but I've painted myself in a mental corner.
Taking a regular URL, split after the ?, which gives a string like variable=val&interesting=something¬interesting=somethingelse I want to extract the value of interesting.
The name of the variable I'm interested in can be a substring of another variable.
So the match should be
either beginning of string or "&" character
followed by "interesting="
followed by the string I want to capture
followed by either another "&" or end of string
I tried something along the lines of
[\^&]interesting=(.*)[&$]
but I got nothing...
Update
This is to be run in a Firefox addon on every get request, meaning that jQuery is not available and if possible I would like to avoid the extra string manipulation caused by writing a function.
To me this feels like a generic "extract part of a string with regex" but maybe I'm wrong (RegEx clearly isn't my strong side)
simple solution
var arr = "variable=val&interesting=something¬interesting=somethingelse".split("&");
for(i in arr) {
var splits = arr[i].split("=");
if(splits[0]=="interesting") alert(splits[1]);
}
also single line match
"variable=val&interesting=something¬interesting=somethingelse".match(/(?:[&]|^)interesting=((?:[^&]|$)+)/)[1]
function getValue(query)
{
var obj=location.search.slice(1),
array=obj.split('&'),
len=array.length;
for(var k=0;k<len;k++)
{
var elm=array[k].split('=');
if(elm[0]==query)return elm[1];
}
}
This function directly extract the query URL and return the corresponding value if present.
//usage
var get=getValue('interesting');
console.log(get);//something
If you're using the Add-on SDK for Firefox, you can use the url module:
https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/url.html
This is much better than using regex.