Agency FB " /> Agency FB " /> Agency FB "/>

Strip a sentence with [bold] tags

93 Views Asked by At

I need to make a script which generates this code:

<CharacterStyleRange FillColor="">
    <Properties>
        <AppliedFont type="string">Agency FB</AppliedFont>
    </Properties>
    <Content>Hallo</Content>
</CharacterStyleRange>

<CharacterStyleRange FillColor="" FontStyle="Bold">
        <Properties>
            <AppliedFont type="string">Agency FB</AppliedFont>
        </Properties>
    <Content>ik</Content>
</CharacterStyleRange>

<CharacterStyleRange FillColor="">
        <Properties>
            <AppliedFont type="string">Agency FB</AppliedFont>
        </Properties>
    <Content>ben een zin</Content>
</CharacterStyleRange>

<CharacterStyleRange FillColor="" FontStyle="Bold">
        <Properties>
            <AppliedFont type="string">Agency FB</AppliedFont>
        </Properties>
    <Content>Met bold</Content>
</CharacterStyleRange>

From this:

Hallo [bold]ik[/bold] ben een zin [bold]met bold[/bold]

Basically what I want is to strip the sentence with the [bold] [/bold] tags, and make a new (with all the other items as shown) for it.

I've tried a couple things but haven't been able to get it properly working, so now I am asking it here.

The language which it has to be transferred into is IDML(This language is used to build InDesign documents)

2

There are 2 best solutions below

2
benjaminz On

I think you could use Regular expression to extract all these text enclosed in [bold].

var exp = "[bold]ik[/bold]";
var re = exp.match(/\[bold\](.*)\[\/bold\]/); //=> Array [ "[bold]ik[/bold]", "ik" ]
return re[1];

You will get "ik".

0
Andi Giga On

Maybe this resource is helpful for you: https://regex101.com/r/xU5jO9/2 There is my regex explained on the right side.

Try this in your console:

var myString = "Hallo [bold]ik[/bold] ben een zin [bold]met bold[/bold]";
var regex = new RegExp(/(\[bold\]?)(.*?)(\[\/bold\]?)/gi);
var replacePattern = "<CharacterStyleRange FillColor=\"\">     <Properties>         <AppliedFont type=\"string\">$2</AppliedFont>     </Properties>     <Content>Hallo</Content> </CharacterStyleRange>";

myString.replace(regex, replacePattern);

I haven't testet it but outside the console it should keep the line breaks. Otherwise open up your project in a text editor like sublime and do the same there. Better make a copy/ git commit first. In Sublime use \2 instead of $2.

EDIT: I added the ? non greedy operator, otherwise it will match form the first bold opening tag to the last closing tag.