wiz khalifa
wiz khalifa

Reputation: 65

Regex for finding multiple spaces after certain content

I need to get the number of all the spaces after the specific word (<{varx}>). Here are my strings.

Point P is (<{var1}>,<{var2}>) and point Q is (<{var3}>,<{var4}> ). What is the slope of line PQ?

It doesn’t matter that which coordinates represent point 1 and point 2, frac{4} but the choice must remain consistent when applying the formula. This problem let us use point P as point 1 x1 = %{<{var1}>|n}%

After finding the word with spaces I need to trim that word and replace it with original string.

trimFunction(data) {
    let tags = data.match(/<{var\d+}>/gm)
    let tag = [];
    if(tags) {
        tags.map(function(item, index) {
            console.log('irem',item);
            tag = item.trim();
            data = data.replace(tags,tag);
            console.log('original data',data);
        });
    }
    return data;
}

I tried this but not able to get the spaces, also didn't get any luck from other questions, The spaces are not showing in question but affecting my XML so I want to remove all the spaces after that certain word. Note: There can be different no. of spaces after <{var3}> any number in place of 3.

Upvotes: 3

Views: 72

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

You may use

data = data.replace(/(<{var\d+})\s+>/g, '$1')

The pattern means

  • (<{var\d+}) - Group 1: <, {var, 1+ digits, }
  • \s+ - 1+ whitespaces
  • > - a > char.

The $1 only keeps the contents of Group 1 in the result.

Upvotes: 1

Related Questions