Jquery Find and Replace Number
I am really struggling to find a solution to this i want to find every instance of a number in a paragraph of text.
<div id='text'>
this is the number 20px and this is the number 100 and this is the number 10.
</div>
So i want to take this and have it ouput the following. 20 100 10
<script type="application/javascript">
$(document).ready(function() {
var text = $('.text').text().toString().search( new RegExp( /^[0-9]+开发者_JS百科$/ i ) );
alert(text);
});
</script>
from the alert i just want it to output the numbers in the text i.e 20 100 10, i now this is way off but any help to put me in the right direction i am banging my head against a wall, thanks.
I don't know where you would replace anything, but to find all numbers, something like this is enough:
// results an array of numbers
var results = $('#text').text().match(/\d+/g);
$('#text').html().match(/\-?\d+(\.\d+)?/g).join(' ')
creates a string with all numbers found inside the div element.
to get an array of all numbers found just remove the .join()
method
Plase note that you have 'text' as id of your element, so you need refer to it as $('#text')
and not as $('.text')
If you use this:
var txt = $( '#text' ).html().match(/\d+/g)
it will give you an array of the numbers
精彩评论