webdesign/JavaScript
How to apply css to only numbers in a text inside any element?
yunsoo.note
2021. 1. 15. 02:02
stackoverflow.com/questions/6097305/replace-nonnumeric-characters-with-javascript
\D matches anything that isn't a number; \d matches a number.
This below will remove all non-numeric characters in a given string:
myString = myString.replace(/\D/g,"");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
.numbers
{
color:red;
font-size:30px;
}
</style>
</head>
<body>
<p>
View 11 new out of 1asdfasdf1241234 message(s) in your inbox. does it work?
</p>
<script type="text/javascript">
$('p').html(function(i,ab) {
return ab.replace(/\d+/g, function(v){
return "<span class='numbers'>" + v + "</span>";
});
});
</script>