JSFiddle - React, Tailwind, and code Playground

by nxhoaf

JavaScript

var str = '<!ENTITY owl "http://www.w3.org/2002/07/owl#" ><!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" ><!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" ><!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >'

var getNamespace = function(input) {
    var result = []; // Store the final result
    var temp = []; // Store the temporary result
    
    // Non trivial case
    if ((input != null) && (input.length != 0)) {
        
        // Get the begin and and index to extract the entity's content
        var begin = input.indexOf("<!ENTITY");
        var end = input.indexOf(">");
        
        if ((begin == -1) || (end == -1) || (begin >= end)) { // not found
            return null;
        }
        
        // Fix the begin index
        begin = begin + "<!ENTITY".length;
        
        // Get the content and save it to nameSpace variable
        var item = input.substring(begin, end); // ok, get one item
        // As end > begin, item is always != null
        item = item.trim(); // Normalize
        result.push(item);
        
        // Continue searching with the rest with
        // recursive searching
        temp = getNamespace(input.substring(end + 1)); 
        
        // Ok, collect all of data and then return
        if (temp != null) {
            for (var i = 0; i < temp.length; i++) {
                result.push(temp[i]);
            }
        }
        return result;
    } else { // Trivial case
        return null;
    }
}

// Parse it to get the result
result = getNamespace(str);
console.log("*************");
for (var i = 0; i < result.length; i++) {
    document.write(result[i] + "<br>");
}