Skip to main content
Code Review

Return to Question

edited tags
Link
Michael K
  • 2.9k
  • 1
  • 22
  • 24

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" 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.

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.

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.

Source Link
Anil
  • 220
  • 2
  • 10

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.

default

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