0

I have a chain like this of get page

file.php?Valor1=one&Valor2=two&Valor3=three

I would like to be able to delete the get request parameter with only having the value of it. for example , remove two Result

file.php?Valor1=one&Valor3=three

Try with

stringvalue.replace(new RegExp(value+"[(&||\s)]"),'');
1
  • Can you include the RegExp that you tried at Question? Commented Apr 15, 2017 at 4:55

3 Answers 3

5

Here's a regular expression that matches an ampersand (&), followed by a series of characters that are not equals signs ([^=]+), an equals sign (=), the literal value two and either the next ampersand or the end of line (&|$):

/&[^=]+=two(&|$)/

let input = 'file.php?&Valor1=one&Valor2=two&Valor3=three';
let output = input.replace(/&[^=]+=two/, '');

console.log(output);

If you're getting the value to be removed from a variable:

let two = 'two';
let re = RegExp('&[^=]+=' + two + '(&|$)');

let input = 'file.php?&Valor1=one&Valor2=two&Valor3=three';
let output = input.replace(re, '');

console.log(output);

In this case, you need to make sure that your variable value does not contain any characters that have special meaning in regular expressions. If that's the case, you need to properly escape them.

Update
To address the input string in the updated question (no ampersand before first parameter):

let one = 'one';
let re = RegExp('([?&])[^=]+=' + one + '(&?|$)');

let input = 'file.php?Valor1=one&Valor2=two&Valor3=three';
let output = input.replace(re, '$1');

console.log(output);

Sign up to request clarification or add additional context in comments.

3 Comments

If the value two , comes from a variable, how would the concatenation be?
Don't know what you mean.
Sorry, error when writing the question was an & more, if I want to delete the value one, do not perform the replace. Can be by that character of more
1

You can use RegExp constructor, RegExp, template literal &[a-zA-Z]+\\d+=(?=${remove})${remove}) to match "&" followed by "a-z", "A-Z", followed by one or more digits followed by "", followed by matching value to pass to .replace()

var str = "file.php?&Valor1=one&Valor2=two&Valor3=three";

var re = function(not) {
           return new RegExp(`&[a-zA-Z]+\\d+=(?=${not})${not}`)
         }

var remove = "two";

var res = str.replace(re(remove), "");

console.log(res);

var remove = "one";

var res = str.replace(re(remove), "");

console.log(res);

var remove = "three";

var res = str.replace(re(remove), "");

console.log(res);

Comments

0

I think a much cleaner solution would be to use the URLSearchParams api

var paramsString = "Valor1=one&Valor2=two&Valor3=three"
var searchParams = new URLSearchParams(paramsString);

//Iterate the search parameters.
//Each element will be [key, value]
for (let p of searchParams) {
    if (p[1] == "two") {
        searchParams.delete(p[0]);
    }
}

console.log(searchParams.toString()); //Valor1=one&Valor3=three

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.