How to split a string in jQuery or JavaScript ?

A JQuery method allows you to separate a character string according to a specific character.

How to split a string in jQuery or JavaScript ?

A JQuery method allows you to separate a character string according to a specific character.

JavaScript provides several methods for handling strings. Let's take a date in French format present in a division. We want to retrieve the day to display it above the month and the year:

<div id=date>01/01/2015</div>

We will use the text() method of JQuery , which allows you to retrieve the text content of an element. To separate a character string according to a specific character, we use the split method. This method will separate the content into an array. Once the string is decomposed, JQuery's HTML() method modifies the HTML content of an element. All you have to do is rewrite the content with a line break (<br/> tag):

var array = $('#date').text().split('/practice/');
/* The array variable then breaks down as follows:
* array[0] => 01
* array[1] => 01
* array[2] => 2015 */
$(#date).HTML(<span> + array[0] + </span></br> + array[1] + / + array[2]);
<!-- Result in HTML -->
<div><span>01</span><br/>01/2015</div>
 

What's Your Reaction?

like
1
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0