Add HTML tags inside string


#1

Hi all ! Could anyone suggest please, is it possible in Ionic2 add such kind string to for example ?

"SERVI<span class='search_result'>c</span>E BULLETINS"


#2

Assuming I understood you correctly, here’s how:

let stringwithhtml = "SERVI<span class='search_result'>c</span>E BULLETINS";
<div [innerHTML]="stringwithhtml"></div>

#3

mich356c, thanks, it working.


#4

mich356c, Is it possible where I do not need, cut all html from string ?


#5

Check out this: How to remove HTML tags from content?

Edit: It’s really only this part which is relevant

strip(html: string) {
	return html.replace(/<(?:.|\n)*?>/gm, '');
}

You can add that to a pipe if you want, so it’s just {{stringwithhtml | stripHTML}}

import { Injectable, Pipe } from '@angular/core';

@Pipe({
	name: 'stripHTML'
})
@Injectable()
export class stripHTML {
	transform(html: string) {
		return html.replace(/<(?:.|\n)*?>/gm, '');
	}
}

#6

I think regular expressions are not sufficiently capable of parsing HTML. If you’re really serious about this, I would look into incorporating a real HTML parser.


#7

mich356c, It help, thanks.


#8

How to add inside string? It appears but the can’t be checked or unchecked.