5

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, '');
};
7
  • You must show what you have tried so far Oct 4, 2021 at 8:22
  • @SubhashisPandey i updated and added some piece of code that I've tried. I don't want to put the whole code, because it lengthen the question description and does make sense anyway. Oct 4, 2021 at 8:27
  • 1
    is it possible that tags are spread among two or more parts in the sentence? For instance, t1 starting in parts[0] and closing in parts[2] ?
    – thchp
    Oct 4, 2021 at 8:29
  • 1
    This is not your main problem, but you have to escape all the ' in the strings. E.g. 'Sometime that\'s just the way'. Otherwise, your code won't work. Oct 4, 2021 at 8:30
  • 1
    @thchp no sir. it couldn't. we trap that before saving to DB. Oct 4, 2021 at 8:31

3 Answers 3

4

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

3
  • ohh wait sir. it does not accept 2 digits tags. e.g. [t12]word[/t12] ?? Oct 4, 2021 at 9:40
  • 1
    just update the regex /\[t\d+\]([^[]*)\[\/t\d+\]/g
    – thchp
    Oct 4, 2021 at 9:56
  • I've edided the post to include support for multiple numbers in the tags with regex from @thchp
    – Jesper
    Oct 4, 2021 at 10:16
3

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".

3
  • thanks for this sir. i'll try this. for a moment. Oct 4, 2021 at 9:28
  • Also I just benchmarked both solutions, and this one is probably faster. Tho it's surprising as standard array looping is supposed to be faster then .map .forEach
    – Hekmatyar
    Oct 4, 2021 at 9:29
  • Sure, benchmarked his solution as well. It's definitely fastest out of all of them.
    – Hekmatyar
    Oct 4, 2021 at 9:34
2

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)
1
  • thanks sir. it worked. however, i will try to study and analyze. each flow, and come up with the output. Oct 4, 2021 at 8:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.