Skip to content

Instantly share code, notes, and snippets.

@schmidtsonian
Last active August 29, 2015 13:58
Show Gist options
  • Save schmidtsonian/9958152 to your computer and use it in GitHub Desktop.
Save schmidtsonian/9958152 to your computer and use it in GitHub Desktop.
Find character and wrap the word(s) containing it
/**
*
* Verifica si existe la funcion, sino la crea (solo existe en firefox)
*
**/
if(!('contains' in String.prototype)){
String.prototype.contains = function( str, startIndex ) {
return -1 !== String.prototype.indexOf.call( this, str, startIndex );
};
};
/*
*
* Find character and wrap the word(s) containing it
*
* @params character - string
* @params tag - string, nombre de etiqueta html
*
**/
String.prototype.wrapp = function( character, tag ) {
var text = "", split = this.split( " " );
for( var i = 0; i <= split.length - 1; i++ ) {
if( split[ i ].contains( character ) ){
split[ i ] = "<" + tag + ">" + split[ i ] + "</" + tag + ">";
};
text = text + split[ i ] + " ";
};
return text;
};
var text = "this #is a text";
text = text.wrapp( "#", "span" ); //this <span>#is</span> a text"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment