Skip to content

Simple HTML to JSON parser use Regexp and String.indexOf

License

Notifications You must be signed in to change notification settings

henryluki/html-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

html-parser

Simple HTML to JSON parser use Regexp and String.indexOf

Install

npm install htmlstr-parser

Basic usage

var html = "<div style='height:10rpx;width: 20rpx;'>1<p>2<br/><a href='http://www.baidu.com'>3</a></p><p>2</p></div>"
htmlParser(html)

Output

{
  "tag": "root",
  "children": [{
    "type": "Element",
    "tagName": "div",
    "attributes": {
      "style": "height:10rpx;width: 20rpx;"
    },
    "children": [{
      "type": "Text",
      "content": "1"
    }, {
      "type": "Element",
      "tagName": "p",
      "attributes": {},
      "children": [{
        "type": "Text",
        "content": "2"
      }, {
        "type": "Element",
        "tagName": "br"
      }, {
        "type": "Element",
        "tagName": "a",
        "attributes": {
          "href": "http://www.baidu.com"
        },
        "children": [{
          "type": "Text",
          "content": "3"
        }]
      }]
    }, {
      "type": "Element",
      "tagName": "p",
      "attributes": {},
      "children": [{
        "type": "Text",
        "content": "2"
      }]
    }]
  }]
}