Detect if a string contains a table

I have a question regarding checking the string.

The string is from a ckeditor so user can input anything.

  • Prevent attributes from being copied when entering new paragraph?
  • Paste from word with uppercase
  • In CkEditor, how do I apply a stylesheet while I'm currently editing?
  • Can't place extraPlugin to right place in toolbar of CKEDITOR
  • Can I use CKEditor without a toolbar?
  • Remove all CKEditor instances
  • 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!

  • CKFinder how to get dimension URL and Dimension (width/height) when choosing an image (files:choose)?
  • How to insert data into Yii framework 2.0 CKEditor extension using JavaScript?
  • CKEditor - disable image drag and drop
  • Find ckeditor instance
  • Toggle the State of a CKEditor plugin button
  • CKEditor - Modify BBCode Output
  • 6 Solutions collect form web for “Detect if a string contains a table”

    String.indexOf() returns a primitive numeric value, specifically:

    the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex or -1 if 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));
    

    Use –

    if(htmlData.indexOf('</table>') > -1){
             console.log('table detected')
    }
    

    or you can find for any tag using jQuery –

    var el = $("<div>"+htmlData+"</div>");
    if(el.find("table").length>0){
        console.log("it contains table");
    }
    

    it would work for any tag, class, id or any css selector.

    var el = $(htmlData);
    if(el.find(".some-class").length>0){
        console.log("it contains some-class");
    }
    

    why the .length?

    if(htmlData.indexOf('</table>') > -1){
             console.log('table detected')
        }
    

    This should work fine. indexOf returns the index (-1) if not found, not an array so length property is not defined

    IndexOf has no property length. As the name “index” indicates, it gives you the index.
    Besides: Why only check, if the user entered an end-tag? You should also check for the start tag. And then- why not using a RegEx like:

    /<table>.*?<\/table>/.test(htmlData)
    

    To test for both?.

    CAVE! This RegEx is not checking if the user entered a valid html-Table-tag. It’s just a stupid check for the occurrence of < table > resp. < /table >.

    It would be silly to add to the collection of same answers so, how about this method using match, this will tell you how many tables there are in that string.

    var string = htmlData.replace(/\s/g, ""); 
    // Trim all whitespace..
    
    var matches = string.match(/<\/table>/g); 
    // Will return 1 for your code and 2 for the demo
    

    Then you would check it like so

    if( matches > 0 ) {
      // There is at least 1 table here
    }
    

    Demo