简体   繁体   English

用javascript替换字符串中的多次出现

[英]replace multiple occurences in a string with javascript

I have a selectbox with parameters as the value in the option, set like this:我有一个带有参数的选择框作为选项中的值,设置如下:

<option value="{$i.tileid}androoftiletypeeq{$i.model}andproducenteq{$i.producent}">{$i.name} {$i.$title}</option>

I am trying to replace all "and" and "eq" to "&" and "=", but I can only get my javascript to replace the first occurrence.我正在尝试将所有“and”和“eq”替换为“&”和“=”,但我只能让我的 javascript 替换第一次出现。 The form is named / ID'ed "rooftile_select该表单被命名/标识为“rooftile_select

$("#rooftile_select").change(function(event) {

  event.preventDefault(); 

  var data = $("#rooftile_select").serialize();                
  var pathname = window.location;
  var finalurl = pathname+'&'+data;

  var replaced = finalurl.replace("and", "&").replace("eq", "=");
});

The last parameters in finalurl then looks like this: finalurl 中的最后一个参数如下所示:

&rid=56&rooftiletype=9andproducenteqs

Am I missing something?我错过了什么吗?

var replaced = finalurl.replace(/and/g, '&').replace(/eq/g, '=');

This should do the trick. 这应该可以解决问题。 With the g after the / you're saying that you want to replace all occurences. /后面加g表示您要替换所有出现的情况。

You can use regexp with global flag 您可以将regexp与全局标志一起使用

var finalurl = '{$i.tileid}androoftiletypeeq{$i.model}andproducenteq{$i.producent}';
finalurl.replace(/and/g, "&").replace(/eq/g, "=")

If your string is always going to contain {...} variables in it, you can use following to avoid accidently replacing the variable or request parameter name 如果您的字符串中总是包含{...}变量,则可以使用以下命令来避免意外替换变量或请求参数名称

finalurl.replace(/\}and/g, "}&").replace(/eq\{/g, "={")

尝试这个 :

replaced = finalurl.replace(/and/g, "&").replace(/eq/g, "=");

With ES12使用 ES12

As of August 2020, modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 ( ES12 ) language specification.截至 2020 年 8 月,现代浏览器支持由 ECMAScript 2021 ( ES12 ) 语言规范定义的String.replaceAll()方法。

var replaced = finalurl.replaceAll('and', '&').replaceAll('eq', '=');

Otherwise否则

We can do a full replacement only if we supply the pattern as a regular expression只有当我们将模式作为正则表达式提供时,我们才能进行完全替换

var replaced = finalurl.replace(/and/g, '&').replace(/eq/g, '=');

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

相关问题 加入并替换多次出现的字符串 - Join and replace on multiple occurences of string Javascript:用带有正则表达式的&#39;b&#39;替换字符串中&#39;a&#39;的所有出现 - Javascript: Replace all occurences of ' a ' in a string with ' b ' with a regex jQuery / Javascript替换多次出现不起作用 - jQuery / Javascript replace multiple occurences not working 替换所有出现的字符,除非字符是字符串中的最后一个字符(javascript) - Replace all occurences of a char except if it is the last character in a string (javascript) 如何使用javascript替换字符串中变量的所有出现? - How to replace all occurences of a variable in a string using javascript? 使用Javascript捕获多次出现的同一字符串的相应索引 - Capture respective index of same string with multiple occurences using Javascript JavaScript字符串多个替换 - JavaScript String Multiple Replace Javascript Regex-替换所有出现的匹配字符串,但方括号之间的任何字符串除外 - Javascript Regex - Replace all occurences of matching string EXCEPT any string between brackets 替换字符串中多次出现的缩写 - Replacing multiple occurences of abbreviations in a string Javascript-查找字符串中所有出现的方括号,并根据其内容替换内容 - Javascript - Find all occurences of brackets in a string and replace the content depending on it's content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM