-1

I have a title in the following format - test ^TM^ title test. I want to convert this text in such a way that the word enclosed between '^' as superscript without changing its position, as shown below.

testTMtitle test

Please help me.

Don
  • 37
  • 1
  • 4

4 Answers4

2

A regex can do this for you, see the example below.

const
  input = 'test ^TM^ title test',
  // This regex will match any text between two "^" characters. The text
  // between the "^" will be placed inside a capture group.
  regex = /\^(.*?)\^/g,  
  // This replaces the match with the text from the capture group, wrapped in a sup tag.
  htmlString = input.replace(regex, `<sup>$1</sup>`);
  
console.log(htmlString);
document.getElementById('output').innerHTML = htmlString;
<div id="output"></div>
Thijs
  • 2,341
  • 2
  • 14
  • 22
  • `(.*?)` what is need of '?' ? – yajiv Sep 17 '18 at 07:05
  • Its makes it lazy. If you have this as input `this ^is^ a ^test^` and you drop the `?` from the regex it will find just one match: `^is^ as ^test^`. Adding the `?` results in getting two matches: `^is^` and `^test^`. You can try it [here](https://regex101.com/r/H1Hu3H/1/). – Thijs Sep 17 '18 at 07:13
  • Okay, got it. Thanks – yajiv Sep 17 '18 at 08:05
  • Not sure what you mean. In my example it is converted to HTML when it is assigned to the `innerHTML` of the `div` element. You’re looking for something else? – Thijs Sep 19 '18 at 04:30
  • I wanted to convert it in template, since I am using angular2. It is not working in select option loop – Don Sep 19 '18 at 04:33
  • Does [this](https://stackoverflow.com/a/34585513/1244780) help? I don’t know Angular myself. – Thijs Sep 19 '18 at 04:36
0

Using javascript:

text = "test ^TM^ title test";
text.replace(/\^(.+?)\^/g,'$1'.sup());
GuyML
  • 257
  • 2
  • 10
  • It's a creative answer, I never knew about the `sup` method. I looked it up on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/sup) and it mentions it has been removed from the web standard. That would make it not a very future-proof solution. – Thijs Sep 17 '18 at 07:02
  • Yep, it basically adds the tags, the replaced text could be `$1` aswell. – GuyML Sep 17 '18 at 07:05
  • This ans exactly helps me...Thank you so much. – Don Sep 17 '18 at 08:05
  • Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer. see: [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) for more information. – GuyML Sep 17 '18 at 09:07
0

I thinks you need to open your files in any HTML or PHP editor and use find and replace option. For example: find ^TM^ Replace with: TM

Chetan
  • 133
  • 1
  • 5
  • 17
0
string = "hello^TM^ hi";
isOdd = true;
newString  = "";
for(i=0; i<string.length; i++){
    if(string[i] == '^'){
        if(isOdd){
            newString += "<sup>";
        } else {
            newString += "</sup>";
        }
        isOdd = !isOdd;
    } else{
        newString += string[i];
    }
}
//newString will have "hello<sup>TM</sup> hi"
Aagam Jain
  • 1,546
  • 1
  • 10
  • 18