How do I get the Text content of a Tag?

0 favourites
  • 4 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • I'm trying to replace text matching a Tag with another text, But the content in the tag is dynamic.

    For example, in this example

    #open=bbcode-tag-range

    xxxxxx [tag=mytag][u]this bit[/u][/tag] xxxxxx
    

    I want use "mytag" to get this bit

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Changing the contents of just part of the text is not currently supported, but you can achieve a similar result another way, by building up the string with variables. For example "xxxxx [tag=mytag]" & SomeVariable & "[/mytag] xxxxx", and then changing SomeVariable and updating the text to that expression again will have the same result as if the contents of the tag were changed.

  • It would be a very useful feature. Especially as you tend to have stuff you want to dynamically replace like this in string form inside a internationalization JSON.

    Still a lot of times you can use replace()

  • I did it using regex. file.c3p

    Functions.TagAt(text, "tag", 0)
    Functions.TagTokenAt(text, "tag", 1, 1)
    Functions.TagTokenCount(text, "tag")
    Functions.TagReplace(text, "tag", "replace", 0)
    

    Eventsheet cheatsheet

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"group","disabled":false,"title":"BBCode Match","description":"","isActiveOnStart":true,"children":[{"functionName":"TagAt","functionDescription":"","functionCategory":"","functionReturnType":"string","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"text","type":"string","initialValue":"","comment":"The text to search within."},{"name":"tag","type":"string","initialValue":"","comment":"The BBCode tag name to match."},{"name":"index","type":"number","initialValue":"0","comment":"Zero-based index of the tag occurrence to retrieve."}],"eventType":"function-block","conditions":[],"actions":[{"type":"comment","text":"example: This is [u][tag]Hello[/tag][/u] string.\nTagAt(text, \"tag\", 0)\nreturn: [b]Hello[/b]"},{"type":"script","script":"const {text, tag, index} = localVars;\nconst result = matchBBCode(text, tag, index);\nruntime.setReturnValue(result.content);"}]},{"functionName":"TagTokenAt","functionDescription":"","functionCategory":"","functionReturnType":"string","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"text","type":"string","initialValue":"","comment":"The text to search within."},{"name":"tag","type":"string","initialValue":"","comment":"The BBCode tag name to match."},{"name":"index","type":"number","initialValue":"0","comment":"The occurrence index of the tag to replace. 0 indicates replace all; 1 for the first occurrence, and so on."},{"name":"part","type":"number","initialValue":"0","comment":"Zero-based index of the part to return."}],"eventType":"function-block","conditions":[],"actions":[{"type":"comment","text":"example: This is [u][tag]Hello[/tag][/u] string.\nTagTokenAt(text, \"tag\", 0, 1)\nreturn: [b][tag]Hello[/tag][/b]"},{"type":"script","script":"const {text, tag, index, part} = localVars;\nconst result = splitBBCode(text, tag, index, part);\nruntime.setReturnValue(result);"}]},{"functionName":"TagTokenCount","functionDescription":"","functionCategory":"","functionReturnType":"number","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"text","type":"string","initialValue":"","comment":"The text to search within."},{"name":"tag","type":"string","initialValue":"","comment":"The BBCode tag name to match."}],"eventType":"function-block","conditions":[],"actions":[{"type":"comment","text":"example: a [u][tag]Hello[/tag][/u] and [u][tag]World[/tag][/u] string.\nTagTokenCount(text, \"tag\")\nreturn: 2"},{"type":"script","script":"const {text, tag} = localVars;\nconst result = tokenCountBBCode(text, tag);\nruntime.setReturnValue(result);"}]},{"functionName":"TagReplace","functionDescription":"","functionCategory":"","functionReturnType":"string","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"text","type":"string","initialValue":"","comment":"The text to search within."},{"name":"tag","type":"string","initialValue":"","comment":"The BBCode tag name to match."},{"name":"rep","type":"string","initialValue":"","comment":"Content to replace the matched tag with; supports `$1` for the parameter and `$2` for the content."},{"name":"index","type":"number","initialValue":"0","comment":"Occurrence index of the tag to replace; `0` replaces all, `1` for the first, and so on."}],"eventType":"function-block","conditions":[],"actions":[{"type":"comment","text":"example: This is [u][tag]Hello[/tag][/u] string.\nTagReplace(text, \"tag\", [b]\"$2 World\"[/b], 0)\nreturn: This is [b]Hello World[/b] string."},{"type":"script","script":"const {text, tag, rep, index} = localVars;\nconst result = replaceBBCode(text, tag, rep, index);\nruntime.setReturnValue(result);"}]}]}]}

    ImportsForEvents.js

    /**
     * Matches all occurrences of a specified BBCode tag in the text and returns an array of match results.
     *
     * @param {string} text - The input text to search within.
     * @param {string} tagName - The BBCode tag name to match.
     * @returns {Array} Array of match result objects, each containing tagName, param, and content.
     */
    function matchAllBBCode(text, tagName) {
     const regex = new RegExp(`\[${tagName}(?:=([^\\]]+))?\\]([\\s\\S]*?)\[\\/${tagName}\\]`, 'g');
     return [...text.matchAll(regex)].map(match => ({
     tagName,
     param: match[1] || null,
     content: match[2],
     }));
    }
    
    
    /**
     * Matches the BBCode tag at the specified index in the text and returns the match result.
     *
     * @param {string} text - The input text to search within.
     * @param {string} tagName - The BBCode tag name to match.
     * @param {number} index - Zero-based index of the tag occurrence to retrieve.
     * @returns {Object|null} Match result object or null if not found.
     */
    function matchBBCode(text, tagName, index) {
     return matchAllBBCode(text, tagName)[index] || null;
    }
    
    
    /**
     * Replaces specified BBCode tags in the text with new content, supporting references to matched groups.
     *
     * @param {string} text - The original text to search within.
     * @param {string} tagName - The BBCode tag name to match.
     * @param {string} replacement - Content to replace the matched tag with; supports `$0` for the full match, `$1` for the parameter, and `$2` for the content.
     * @param {number} index - Occurrence index of the tag to replace; `0` replaces all, `1` for the first, and so on.
     * @returns {string} Text with the specified BBCode tags replaced.
     */
    function replaceBBCode(text, tagName, replacement, index) {
     const regex = new RegExp(`\[${tagName}(?:=([^\\]]+))?\\]([\\s\\S]*?)\[\\/${tagName}\\]`, 'g');
     let matchCount = 0;
     return text.replace(regex, (match, p1, p2) => {
     matchCount++;
     return (index === 0 || matchCount === index)
     ? replacement
    	 .replace(/\$0/g, match)
    	 .replace(/\$1/g, p1 || '')
    	 .replace(/\$2/g, p2)
     : match;
     });
    }
    
    
    /**
     * Splits the text based on the specified BBCode tag and retrieves parts according to the tagIndex and partIndex.
     *
     * @param {string} text - The original text to split.
     * @param {string} tagName - The BBCode tag name to match.
     * @param {number} tagIndex - Specifies which tag occurrence to split by. `0` for all matches, `1` for the first tag only.
     * @param {number} partIndex - The part to return: 0 for the text before the tag, 1 for the tag itself, and 2 for the text after the tag.
     * @returns {string|null} The extracted part or null if not found.
     */
    function splitBBCode(text, tagName, tagIndex, partIndex) {
     const regex = new RegExp(`(\[${tagName}(?:=[^\\]]+)?\\][\\s\\S]*?\[\\/${tagName}\\])`, 'g');
     const parts = text.split(regex).map(part => part.trim());
    
     if (tagIndex === 0) {
     return parts[partIndex] || null;
     }
    
     if (tagIndex === 1) {
     if (partIndex === 0) return parts[0];
     if (partIndex === 1) return parts[1];
     if (partIndex === 2) return parts.slice(2).join(' ');
     }
     return null;
    }
    
    
    /**
     * Counts the occurrences of a specified BBCode tag in the text.
     *
     * @param {string} text - The original text to search within.
     * @param {string} tagName - The BBCode tag name to count.
     * @param {number} tagIndex - returns the match count;
     * @returns {number} The count based on tagIndex.
     */
    function tokenCountBBCode(text, tagName) {
     const regex = new RegExp(`\[${tagName}(?:=[^\\]]+)?\\][\\s\\S]*?\[\\/${tagName}\\]`, 'g');
     const matches = [...text.matchAll(regex)];
     return matches.length;
    }
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)