1

I have the following script:

$(".balkenclosed").click(function() {
window.location = $(this).find("a").attr("href"); 
return false;
});

I try to add a .replace function like this:

var replace = new Map([
/ [^a-zA-Z0-9\s]/ig, "", $id
]),
id = a;
replace.forEach(function(value, key){
id = id.replace(key, value);
});

It should replace invalid characters in the id of divs like this one:

<div class="balken"><div class="balkenopen"><a id="2our$ / team/2$"
style="text-decoration:none; color:black;" href="#2our$ / team/2$">
our team</a></div></div>

The result in this case should be id:"our_team" and href:"our_team"

My approach doesn't work and I feel like I'm completely wrong. I would really appreciate any hint into the right direction

undefined
  • 169
  • 3
  • 13

2 Answers2

1

Use value as the regex

var replace = [
   /[^a-zA-Z0-9\s]/ig, ""
];
replace.forEach(function(value, key){
  id = id.replace( value, "");
});

In fact, looking at the content of replace, simply

id = id.replace( replace[0], replace[1]);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • thank you for your answer. Am I correct with my guess that you mean with `replace[0], replace[1]` that it replace the first and the second id inside the div? And then just replace the line `id = id.replace( value, "");` with `id = id.replace( replace[0], replace[1]);` ? – undefined Jan 09 '18 at 15:18
  • 1
    No, I meant that you only need to use `id = id.replace( replace[0], replace[1]);` and nothing else. Also observe that `replace`'s initialization has changed from `map` to a simple array. – gurvinder372 Jan 10 '18 at 06:24
1

I have written jquery, which will produce your desired result, you can refine though. Hope this will help you.

Please consider that it will search for entire anchor tag and replace if matches found. Replace $(a) with the more appropriate selector for the specific search.

$('a').each(function() {
  var self = $(this);
  var id = self.attr('id');
  var href = self.attr('href');
  if (id)
    self.attr('id', stripEverything(id, '_'));
  if (href)
    self.attr('href', '#'+stripEverything(href, '_'));
});

function stripEverything(string, replaceSpaceWith) {
  return string.replace(/[^a-zA-Z ]+/g, '').split(' ').filter(function(e) {
    return e
  }).join(replaceSpaceWith);
}

You can look the solution at Fiddle

Rajan
  • 1,463
  • 12
  • 26
  • dear Rajan, thank you very much for your answer and your help. I tested your code and it works well. can you give me a hint how I can keep the leading # in the href? – undefined Jan 10 '18 at 11:24
  • just add `#` in ` self.attr('href', stripEverything(href, '_'));` which will be like `self.attr('href', '#'+stripEverything(href, '_'));` – Rajan Jan 11 '18 at 05:53
  • @undefined I have updated the answer as well as the fiddle, and please accept answer if it satisfies your need and upvote it, :) – Rajan Jan 11 '18 at 05:58
  • dear Rajan, thank you again for your answer and the edit. Due to my low rep (sadly I never get upvotes on my questions) my upvote on your answer wont be not displayed, sorry for that. But I accepted your answer :) you can see my solution here: [https://codepen.io/undefined999/pen/VyyQVw] I dont want to bother you but maybe you have an idea how I can replace special characters like ä with ae and so on? – undefined Jan 11 '18 at 07:29
  • @undefined, please have a look [here](https://stackoverflow.com/questions/36366125/include-special-characters-like-%C3%B6-%C3%A4-%C3%BC-in-regular-expressions) – Rajan Jan 11 '18 at 09:30
  • Thanks Rajan, I found and implemented a working solution already. same pen as above. Thank you very much for your help, you're really a kind and nice guy :) – undefined Jan 11 '18 at 09:32