I have a question regarding checking the string.
The string is from a ckeditor so user can input anything.
The variable name is htmlData and it is like:
test here<br />
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
    <tbody>
        <tr>
            <td>
                111</td>
            <td>
                222</td>
        </tr>
        <tr>
            <td>
                333</td>
            <td>
                444</td>
        </tr>
        <tr>
            <td>
                555</td>
            <td>
                666</td>
        </tr>
    </tbody>
</table>
<br />
second test 
I want to detect if user add a table structure and I have tried
 if(htmlData.indexOf('</table>').length > -1){
             console.log('table detected')
        }
but it doesn't show anything in my console. Can anyone gives a hint on this?
Thanks so much!
String.indexOf() returns a primitive numeric value, specifically:
the
indexwithin the callingStringobject of the first occurrence of the specified value, starting the search atfromIndexor-1if the value is not found.
These primitives have no properties, i.e.: length.
if(htmlData.indexOf('</table>').length > -1){
    console.log('table detected')
}
So, simply remove .length from your code:
if(htmlData.indexOf('</table>') > -1){
    console.log('table detected')
}
                        You can use it:
if(/<table>/i.test(htmlData));
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With