jQuery HTML Element How to - Apply CSS only to numbers in paragraph








Question

We would like to know how to apply CSS only to numbers in paragraph.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<style type='text/css'>
span {<!--from   w w  w  .java 2 s  . c  om-->
  color: blue
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $('p').html(function(index, value) {
        return value.replace(/(\d+)/g, '<span>$1</span>');
    });
});
</script>
</head>
<body>
  <p>Text and numbers: 123</p>
  <p>123 and text</p>
</body>
</html>

The code above is rendered as follows: