简体   繁体   English

删除字符串JavaScript正则表达式中的字符

[英]remove chars in String javascript Regex

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. 我希望能够仅具有其值来删除get request参数。 for example , remove two Result 例如, remove two 结果

file.php?Valor1=one&Valor3=three

Try with 试试看

stringvalue.replace(new RegExp(value+"[(&||\s)]"),'');

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和下一个与号或结尾的字符第(&|$)

/&[^=]+=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); 

You can use RegExp constructor, RegExp , template literal &[a-zA-Z]+\\\\d+=(?=${remove})${remove}) to match "&" followed by "az", "AZ", followed by one or more digits followed by "" , followed by matching value to pass to .replace() 您可以使用RegExp构造函数RegExp模板文字&[a-zA-Z]+\\\\d+=(?=${remove})${remove})匹配"&"后跟“ az”,“ AZ” ,后跟一个或多个数字,后跟"" ,然后匹配要传递给.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); 

I think a much cleaner solution would be to use the URLSearchParams api 我认为更干净的解决方案是使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM