Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get customed tags in a text, and put in another text?

The header question may not be easy to understand. Hope you can understand my detailed info below.

I have sentence data below, that has some tags, represented by [tn]tag[/tn]:

const sentence = `[t1]Sometimes[/t1] that's [t2]just the way[/t2] it has to be. Sure, there
 were [t3]probably[/t3] other options, but he didn't let them [t4]enter his mind[/t4]. It 
was done and that was that. It was just the way [t5]it[/t5] had to be.`

And i have parts of the sentence.

const parts = [
    "Sometimes that's just the way",
    "it has to be",
    "Sure,",
    "there were probably other options,",
    "but he didn't let them enter his mind.",
    "It was done and that was that.",
    "It was just the way it had to be."
];

Goal is to add tags on each parts using the sentence above.

const expectedOutput = [
    "[t1]Sometimes[/t1] that's [t2]just the way[/t2]",
    "it has to be",
    "Sure,",
    "there were [t3]probably[/t3] other options,",
    "but he didn't let them [t4]enter his mind[/t4].",
    "It was done and that was that.",
    "It was just the way [t5]it[/t5] had to be."
];

What I've tried so far are the following, but seemingly does not make sense, and I endup nothing.

  1. make a clone sentence, and remove all tags. (code below)
  2. find all parts in the sentence.
  3. [problem is I don't know how to put again the tags]

I wanna ask is there any chance to achieve it? and how. thanks


export const removeTags = (content) => {
  content = content.replace(/([t]|[\/t])/g, '');
  return content.replace(/([t\d+]|[\/t\d+])/g, '');
};
like image 246
Johnny Avatar asked Oct 04 '21 08:10

Johnny


People also ask

How do I get text between tags in HTML?

The preg_match() function is the best option to extract text between HTML tags with REGEX in PHP. If you want to get content between tags, use regular expressions with preg_match() function in PHP. You can also extract the content inside element based on class name or ID using PHP.

Can I create my own HTML tags?

yes and no. There's a standard set of HTML tags, css elements, and JavaScript code that you can use in any browser, like div and font-weight and alert, but the reason any of it works is a two-step process.

How do you make a HTML tag?

An HTML tag is a special word or letter surrounded by angle brackets, < and >. You use tags to create HTML elements , such as paragraphs or links. Many elements have an opening tag and a closing tag — for example, a p (paragraph) element has a <p> tag, followed by the paragraph text, followed by a closing </p> tag.

How do you define HTML tags?

HTML tags are like keywords which defines that how web browser will format and display the content. With the help of tags, a web browser can distinguish between an HTML content and a simple content. HTML tags contain three main parts: opening tag, content and closing tag. But some HTML tags are unclosed tags.


3 Answers

For a regex answer: /\[t\d+\]([^[]*)\[\/t\d+\]/g will match all words including tags and then group all the words within those tags.

let regex = /\[t\d+\]([^[]*)\[\/t\d+\]/g;
let matches = [], tags = [];
var match = regex.exec(sentence);
while (match != null) {
    tags.push(match[0]);
    matches.push(match[1]);
    match = regex.exec(sentence);
}

now we just need to replace all matches with tags inside of parts

let lastSeen = 0;
for (let i = 0; i < parts.length; i++) {
    for (let j = lastSeen; j < matches.length; j++) {
        if (parts[i].includes(matches[j])) {
            lastSeen++;
            parts[i] = parts[i].replaceAll(matches[j], tags[j])
        } else if (j > lastSeen) {
            break;
        }
    }
}

Here is a link to see the regex: regex101

And here is a JSFiddle to see the whole thing JSFiddle

like image 141
Jesper Avatar answered Oct 19 '22 10:10

Jesper


Here I also made an alternative version, so just gonna dump it below. No nesting as in @thchp but a bit more easy to read imo.

const sentence = "[t1]Sometimes[/t1] that's [t2]just the way[/t2] it has to be. Sure, there" +
 "were [t3]probably[/t3] other options, but he didn't let them [t4]enter his mind[/t4]. It " +
 "was done and that was that. It was just the way [t5]it[/t5] had to be.";

const parts = [
    "Sometimes that's just the way",
    "it has to be",
    "Sure,",
    "there were probably other options,",
    "but he didn't let them enter his mind.",
    "It was done and that was that.",
    "It was just the way it had to be."
];

const getTokens = (text) => {
  const tokens = text.match(/\[t[0-9]+\]/gm);
  const result = [];
  
  tokens.forEach(tokenOpen => {
    const tokenClose = "[/" + tokenOpen.substring(1, tokenOpen.length);
    const tokenStart = text.indexOf(tokenOpen) + tokenOpen.length;
    const tokenEnd = text.indexOf(tokenClose);
    result.push({
        tokenOpen,
      tokenClose,
        value: text.substr(tokenStart, tokenEnd - tokenStart)
    });
  });
  
  return result;
}

const applyTokens = (parts, tokens) => {
    return parts.map(part => {
        const match = tokens.filter(x => part.includes(x.value));

        if(!match.length)
            return part;
    
        const {value, tokenOpen, tokenClose} = match[0];
        const index = part.indexOf(value);
        const partPre =  part.substr(0, index);
        const partPost = part.substr(index + value.length, part.length);
        return partPre + tokenOpen + part.substr(index, value.length) + tokenClose + partPost;
    });
}

const output = applyTokens(parts, getTokens(sentence));

console.log(output);

It appends tags to all occurrences of some value in a part so the first "it" in second element of "parts" array gets wrapped as well. If you don't want that then remove once used token in "applyTokens".

like image 36
Hekmatyar Avatar answered Oct 19 '22 11:10

Hekmatyar


Here is a solution that assumes that there are no nested tags, that all tags open and close in the part. Also, this assumes that all characters from the sentence are in parts. For this last assumption, I had to add the . after it has to be in the second expected part. I also had to remove newline characters from the sentence but I think it was because of the copy/paste. This solution will loop through all characters and store two parallel buffers : one with the tags, one without. We will use the second one to compare with the parts, and use the first one to generate the output.

const sentence = `[t1]Sometimes[/t1] that's [t2]just the way[/t2] it has to be. Sure, there were [t3]probably[/t3] other options, but he didn't let them [t4]enter his mind[/t4]. It was done and that was that. It was just the way [t5]it[/t5] had to be.`


const parts = [
  "Sometimes that's just the way",
  "it has to be.",
  "Sure,",
  "there were probably other options,",
  "but he didn't let them enter his mind.",
  "It was done and that was that.",
  "It was just the way it had to be."
];

let bufferWithoutTags = ""
let bufferWithTags = ""
const output = []
const buffers = []
let tagOpened = false

for (let i = 0; i < sentence.length; ++i) {
  let c = sentence[i]
  bufferWithTags += c
  if ( c === '[') {
    if (tagOpened && sentence[i+1] === "/") {
      tagOpened = false
    } else {
      tagOpened = true
    }
    while (c != ']') {
      c = sentence[++i]
      bufferWithTags += c
    }
  } else {
    bufferWithoutTags += c;
  }
  if (!tagOpened) {
    for (const part of parts) {
      if (part === bufferWithoutTags.trim()) {
        output.push(bufferWithTags.trim())
        bufferWithTags = bufferWithoutTags = ""
      }
    }
  }
}
console.log(output)
like image 32
thchp Avatar answered Oct 19 '22 12:10

thchp