Javascript uses regular expressions to extract the text in the html string and split it with a comma.

for example:

var str=" < div style= "text-align: center;" > < span style= "font-size: 12px;" > Please fill out the form < / div > < div style= "text-align: center;" > < span style= "font-size: 12px;" > We will contact you in 1-2 working days < / div >"

the final result is: please fill out the form carefully and we will contact you within 1-2 working days

< hr >

Thank you, gods!


Hello friend, this is my realization (after work first, I will optimize it later). I hope it will be helpful to you. Welcome to communicate with you.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
    let str='<div style="text-align: center;"><span style="font-size: 12px;"></span></div><div style="text-align: center;"><span style="font-size: 12px;">1-2</span></div>';

    str = str.replace(/\<[^>]*\>(([^<])*)/g, function() {
        let mark = "";
        if(arguments[2]) {
            mark = ",";
        }
        return arguments[1] + mark;
    });
    str = str.substring(0, str.length - 1);

    console.log(str); // ,1-2
    </script>
</body>
</html>

Q: could you write another one for me to put the style style of the string into an array, for example, ['text-align: center','font-size: 12px'], thank you

answer, as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> </title>
</head>
<body>
    <script>
    let str='<div style="text-align: center; line-height: 40px"><span style="font-size: 12px;"></span></div><div style="text-align: center;"><span style="font-size: 12px;">1-2</span></div>';

    let reg = /style=\"(.*?);?\"/g;
    let arr = [];
    str.replace(reg, function() {
        arr.push(arguments[1])
    });

    let newArr = [];
    arr.forEach(item => {
        newArr.push(...item.split(';'));
    })
    console.log(newArr); // ["text-align: center", " line-height: 40px", "font-size: 12px", "text-align: center", "font-size: 12px"]
    </script>
</body>
</html>

str.match(/\^+(?=<)/g);
var result=str.match(/[^>]+(?=<)/g);
result=result?result.join(","):"";
Menu