cgee
cgee

Reputation: 1887

split() function return a string which is NOT splitted

I want to split this string:

120,00 m²

into this:

120 m²

I try it with this code:

var test = jQuery('#wpsight-listing-details-3 .span4:nth-child(4) .listing-details-value').html();

var partsOfStr = test.split(',');

alert(partsOfStr);

The alert returns everytime the 'old' string (120,00 m²).

Can somebody helps me?

Thank you very much!

Upvotes: 1

Views: 70

Answers (4)

Mahesh Durairaj
Mahesh Durairaj

Reputation: 102

var a= '120,0,000,000,00 m²'; a.replace(/[,]\d+/g,' ');

Upvotes: 0

Wesley Smith
Wesley Smith

Reputation: 19571

I would use a regular expression and replace, .replace(/,\d+ m²/, ' m²')

like this:

$('.test').text('120,00 m²'.replace(/,\d+ m²/, ' m²'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test"></div>

Upvotes: 1

Dalorzo
Dalorzo

Reputation: 20014

Another option that covers different numbers after the comma:

   var result= "120,00 m²".replace(/[,]\d+[ ]/,' ');

Upvotes: 1

Jai
Jai

Reputation: 74738

You can do this:

var test = "120,00 m²";
var partsOfStr = test.split(',00').join('');

alert(partsOfStr);

Upvotes: 1

Related Questions