1

I created a filter in angular 1 and have some questions:

I have an string like this:

**StackExchange** is a website...

and the desired outcome would be:

<span class="title">StackExchange</span> is a website...

my filter looks like this:

.filter('makeSpan', function() {
  return function(input, all) {
    return (!!input) ? input.replace(/[*]{2}/, '<span class="title">')  : '';
  }
});

When Applying this filter I get StackExchange** is a website... My question is how can I do so I can target the other 2 **. Thanks.

2
  • Using html elements looks more like that you need a directive to me Commented Oct 27, 2016 at 14:03
  • if data wouldn't come from an API, you'd be right. Commented Oct 27, 2016 at 14:04

2 Answers 2

1

If you HTML input is tag-free, you may "target" both the ** pairs with:

return (!!input) ? input.replace(/\*{2}([^]*?)\*{2}/g, '<span class="title">$1</span>') : '';

See the regex demo

  • \*{2} - **
  • ([^]*?) - Group 1 (referred to with $1) capturing any 0+ chars, as few as possible up to the first...
  • \*{2} - **
Sign up to request clarification or add additional context in comments.

Comments

0

This actually seems to be working.

return (!!input) ? input.replace(/[*]{2}/, '<span class="title">').replace(/[*]{2}/, '</span>')  : '';

1 Comment

It seems you need return (!!input) ? input.replace(/\*{2}([^]*?)\*{2}/g, '<span class="title">$1</span>') : '';

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.