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. The form is named / ID'ed "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:
&rid=56&rooftiletype=9andproducenteqs
Am I missing something?
The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.
To make the method replace() replace all occurrences of the pattern you have to enable the global flag on the regular expression: Append g after at the end of regular expression literal: /search/g. Or when using a regular expression constructor, add 'g' to the second argument: new RegExp('search', 'g')
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.
Try this :
replaced = finalurl.replace(/and/g, "&").replace(/eq/g, "=");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With