exjn
Extract content with simple expression defined in JSON. Javascript Implementation.
Examples
Extract a single text value
// Expression defined in JSON configuration file.var singleExpressionJSON = "contains": "must have content" // sample must have this value to consider extracting "within": // scope content "prefix": "The" "content" // content must be prefixed with "suffix": "to" // content must be suffixed with "trimming": true // trim whitespace of the final content of within // Instantiate an expressionvar exjn = ;var singleExpression = singleExpressionJSON; // The sample to extractvar singleSample = "The sample text must have content XYZ to extract." // Extract out 'XYZ'var result = singleExpression; // result = 'XYZ'
Extract an array
// Expression defined in JSON configuration file.var arrayExpressionJSON = "contains": "Signature" "within": "prefix": "*start" "suffix": "#end" "array": "separator": " | " "element": "within": "prefix": "<span>" "suffix": "</span>" "trimming": true // Instantiate an expressionvar exjn = ;var arrayExpression = arrayExpressionJSON; // The sample to extractvar arraySample = ' | *start Signature<s> 1 </s> | <s> 2 </s>| <s> 3 </s> | error. #end'; // Extract out an array of stringsvar result = arrayExpression; // Please be advised that </s>| <s> between 2 and 3 does not comply with separator " | ".// Therefore only 4 elements are separated by the separator which are// "Signature<s> 1 </s>"// "<s> 2 </s>| <s> 3 </s>"// "error."// After applying within to extract content, only the first two elements meet the spec,// leading the result//// result = ["1", "2"]
Extract a dictionary
// Expression defined in JSON configuration file.var dictionaryExpressionJSON = "dictionary": "title": "within": "prefix": "<span>" "Title:" "suffix": "</span>" "trimming": true "author": "within": "prefix": "<span>" "Author:" "suffix": "</span>" "trimming": true "artist": "within": "prefix": "<span>" "Artist:" "suffix": "</span>" "trimming": true // Instantiate an expressionvar exjn = ;var dictionaryExpression = dictionaryExpressionJSON; // The sample to extractvar arraySample = ' <span>Title: The Title</span> <span> Author:The Author </span><span> Artis:</span>'; // Extract out a dictionaryvar result = dictionaryExpression; // The result should be//// result = {// "title": "The Title",// "author: "The Author"// }//// Be reminded that "Artis:" does not match "Artist:"