Skip to main content
Code Review

Return to Revisions

2 of 3
Actually making it render two spaces

Refactor 2 regular expressions in Javascript into 1 statement

I have the following code, which replaces " " <-- Spaces with "-" <-- Dash.
Because some of the title text will be returned with more spaces, it converts it into 3 dashes.

Examples of Possible Titles:

  • "Hello World From Jquery" <--- Notice 2 Spaces between the words, not 1
  • "Main Title - Sub-Title or description here"

I would like the above titles to be turned into:

  • "Hello-World-From-Jquery" <---- (Not "Hello--World--From--Jquery")
  • "Main-Title-Sub-Title-or-description-here" <--- (Not "Main-Title---Sub-Title-or-desc...")

This is what I got so far, but its not ideal as you can see. Example:

var dirty_url = ui.item.label;
var url = dirty_url.replace(/\s+/g,"-");
var url = url.replace(/---/g,"-");

So basically, I want to convert all spaces and illegal characters (Its for a url) to "-" to be used in a url. I only want a maximum of 1 dash between charactors.

Anil
  • 220
  • 2
  • 10
default

AltStyle によって変換されたページ (->オリジナル) /