最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect if a string contains a table - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share Improve this question asked Jul 29, 2013 at 16:53 FlyingCatFlyingCat 14.3k36 gold badges125 silver badges201 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 9

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

发布评论

评论列表(0)

  1. 暂无评论