Last active
August 29, 2015 14:00
-
-
Save marioblas/e40a5cc4391da1511132 to your computer and use it in GitHub Desktop.
Javascript - Hace mas amigable la lectura de una lista de elementos separados por punto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Hace mas amigable la lectura de una lista de elementos separados por punto | |
* | |
* Ejemplos: | |
* | |
* HTML.CSS.Javascript.Node. | |
* --> HTML, CSS, Javascript y Node. | |
* | |
* HTML. | |
* CSS. | |
* Javascript. | |
* Node. | |
* --> HTML, CSS, Javascript y Node. | |
*/ | |
// Elimina los saltos de linea y espacios + salto de linea | |
str = str.replace(/(\r\n|\n|\r)/gm, ''); | |
// Sustituye los '.' por ', ' excepto la última ocurrencia | |
str = str.replace(/\.(?!$)/g, ', '); | |
// Sustituye la última ocurrencia de ',' por ' y ' | |
str = str.replace(/,\s([^,]+)$/, ' y $1'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment