Skip to content

Instantly share code, notes, and snippets.

@raghunandangupta1992
Last active August 24, 2017 20:08
Show Gist options
  • Save raghunandangupta1992/112a26ae5d1c7ab3c632576b6eac4106 to your computer and use it in GitHub Desktop.
Save raghunandangupta1992/112a26ae5d1c7ab3c632576b6eac4106 to your computer and use it in GitHub Desktop.
requirebin sketch
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
var text = "initial text <b>tag 1 ka text</b> bwala word ka text <b>tag 2 ka text</b> left off text";
var HtmlParser = function(textToParse, tagToPick) {
var openTag = tagToPick;
var closetag = tagToPick[0] + '/' + tagToPick.substr(1);
var splitArray = text.split(openTag);
var textArray = [];
var textObject = [];
if (splitArray[0].indexOf(closetag) === -1) {
textObject.startText = splitArray[0];
splitArray.shift();
} else {
textObject.startText = '';
}
splitArray.forEach(function(txt, ind) {
var secondSplit = txt.split(closetag);
var tempObj = {};
tempObj.msg = secondSplit[0] || '';
tempObj.postText = secondSplit[1] || '';
textArray.push(tempObj);
});
textObject.textArray = textArray;
return textObject;
}
console.log(HtmlParser(text, '<b>'));
setTimeout(function(){var text="initial text <b>tag 1 ka text</b> bwala word ka text <b>tag 2 ka text</b> left off text";var HtmlParser=function(textToParse,tagToPick){var openTag=tagToPick;var closetag=tagToPick[0]+"/"+tagToPick.substr(1);var splitArray=text.split(openTag);var textArray=[];var textObject=[];if(splitArray[0].indexOf(closetag)===-1){textObject.startText=splitArray[0];splitArray.shift()}else{textObject.startText=""}splitArray.forEach(function(txt,ind){var secondSplit=txt.split(closetag);var tempObj={};tempObj.msg=secondSplit[0]||"";tempObj.postText=secondSplit[1]||"";textArray.push(tempObj)});textObject.textArray=textArray;return textObject};console.log(HtmlParser(text,"<b>"))},0);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"htmlparser2": "3.9.2"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment