Remove only anchor tag from string

Uday kumar das

In controller:

str= "Employee <b><a href=http://xyz.localhost.in:3000/admin/company>Uday Das</a></b> has applied for leave."

I want to remove anchor tag from above string like Employee <b>Uday Das</b> has applied for leave.,

I used this code:

ActionView::Base.full_sanitizer.sanitize(str)

But it removes all the html tags from the string, as a result i am getting Employee Uday Das has applied for leave..

NOTE: I am getting strings which is dynamic, anchor tag position is not fixed, it could be anywhere in the string.

shivam

You can use nokogiri gem.

Something like:

require 'nokogiri'
doc = Nokogiri::HTML str
node = doc.at("a")
node.replace(node.text)

puts puts doc.inner_html
# <html><body><p>Employee <b>Uday Das</b> has applied for leave.</p></body></html>

or to match your exact output:

puts doc.at("p").inner_html
# Employee <b>Uday Das</b> has applied for leave.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove anchor tag only, but to keep inner html?

From Dev

jQuery how to remove id tag from anchor

From Dev

Remove border from an image inside an anchor tag

From Dev

jQuery how to remove id tag from anchor

From Dev

Remove underline only from anchor element child

From Dev

Remove a wild "-" dash from active class anchor tag

From Dev

Decoding Anchor Tag in String

From Dev

Extract anchor tag href from string javascript/jquery

From Dev

remove the anchor from col-md-9 and keep it to only image

From Dev

Anchor tag works only in firefox

From Dev

Python3: Remove HTML from string, all examples are simple "tag only" removal

From Dev

R remove only "[" "]" from string

From Dev

Remove anchor from url

From Dev

Remove anchor from url

From Dev

PHP Regex completely remove HTML Anchor link from string in PHP

From Dev

How to check if a string is an anchor tag?

From Dev

How to check if a string is an anchor tag?

From Dev

Remove tag from string variable using Regex

From Dev

Remove extra "a href" tag from html string

From Dev

Remove BR tag from the beginning and end of a string

From Dev

JS Regex to remove IMG Tag from String

From Dev

Remove html tag from a string using jQquery

From Dev

Remove BR tag from the beginning and end of a string

From Dev

Remove tag from string variable using Regex

From Dev

Remove HTML tag from string in AngularJS

From Dev

Remove special character tag from my string

From Dev

remove strong tag and its content from string

From Dev

How to check and remove html tag from string

From Dev

How to remove <p></p> tag from the string