0

I'm trying to make a filter which has a lot of options that get listed in the top of the filter box, now, I'm trying to add the countries and I decided to use regex. But I can't get it to work, for some reason it replaces in the middle of the string instead of the beginning.

JS Code:

function countryCheck(object) {
    var filter = document.getElementById("search_filter");
    var re = new RegExp("<b>Location<\/b>:\s[a-zA-Z\s]+\s\|\s");
    filter.innerHTML = filter.innerHTML.replace(re, ("<b>Location</b>: " + object.value.replace("&nbsp;&nbsp;", "") + " | "));
}

HTML Code:

<div id='search_filter'><b>Filter</b>: <b>Location</b>: Anywhere | <b>Player Slots</b>: >=0/>=0</div>
<br />

<select class='select' style='width:100%;' onchange='countryCheck(this);'>
    <option>Anywhere</option>
    <option>Europe</option>
    <option>&nbsp;&nbsp;Sweden</option>
    <option>&nbsp;&nbsp;Germany</option>
    <option>&nbsp;&nbsp;United Kingdom</option>
    <option>&nbsp;&nbsp;France</option>
    <option>North America</option>
    <option>&nbsp;&nbsp;Canada</option>
    <option>&nbsp;&nbsp;United States</option>
    <option>&nbsp;&nbsp;Mexico</option>
</select>

jsFiddle showing what the problem is: jsFiddle

I'm trying to make so it replaces the current <b>Location</b>: [some country] | with <b>Location</b>: [some other country] |

4
  • 8
    Why would you do this with regex? Why not give the location value some class or id and replace the content directly?
    – Evan Davis
    Commented May 20, 2013 at 14:21
  • Your problem is with \/ and \s See: stackoverflow.com/questions/16611956/… Commented May 20, 2013 at 14:23
  • 1
    Unrelated side note: You might want to take a look at <optgroup>.
    – JJJ
    Commented May 20, 2013 at 14:23
  • Didn't know about optgroup, but still, I want the user to be able to select "Europe". Commented May 20, 2013 at 15:13

4 Answers 4

2

You should not use a regex for this but put your target into a tag of its own like:

<div id="search_filter">
     <b>Filter</b>: <b>Location</b>: 
     <span id="search_filter_location">Anywhere</span>
      | <b>Player Slots</b>: >=0/>=0
</div>

and then replace the content inside this tag only like:

function countryCheck(object) {
    var filter = document.getElementById("search_filter_location");
    filter.innerHTML = object.value.replace("&nbsp;&nbsp;", "");
}
0

If you want to replace in the beginning of your string just use ^ at the beginning of your regex, which matches the beginning ($ matches the end)

So your code would be:

function countryCheck(object) {
    var filter = document.getElementById("search_filter");
    var re = new RegExp("<b>Location<\/b>:\s[a-zA-Z\s]+\s\|\s");
    filter.innerHTML = filter.innerHTML.replace(re, ("<b>Location</b>: " + object.value.replace(/^(\s){2}/, "") + " | "));
}

Edit: If you want to replace any spaces where it's 2 or more spaces atthe begging on your string, you could use this as your regex: /^(\s){2,}/

0

When you make a regular expression object via the RegExp constructor and a string, you must double the backslashes. It's really better to just use native syntax:

    var re = /<b>Location<\/b>:\s[a-zA-Z\s]+\s\|\s/;

In your code, the \s subexpressions were being turned into just plain s by the JavaScript tokenizer as it recognized the string constant.

0

Try

function countryCheck(object) {
    var filter = document.getElementById("search_filter");
    var re = /(<b>Location<\/b>:\s)([^|]+)(|)/;
    filter.innerHTML = filter.innerHTML.replace(re, ("$1" + object.value.replace("&nbsp;&nbsp;", "") + " $3"));
}

Demo: Fiddle

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.