Получить текст, написанный в теге привязки без идентификатора: javascript - PullRequest
0 голосов
/ 25 сентября 2019

У меня есть строка с небольшим содержанием (URL), которую мне нужно удалить или заменить на текст тега.

Ввод:

Entity Framework is an <a href="http://www.google.com">Object </a>/ Relational Mapper (O/RM) that helps you read and write data from and to a database. In this course, Mosh, teaches you the core concepts of Entity <a href="http://www.google.com">Framework</a>through a series of clear, concise and hands-on lectures.

вывод:

Entity Framework is an Object / Relational Mapper (O/RM) that helps you read and write data from and to a database. In this course, Mosh, teaches you the core concepts of Entity Framework through a series of clear, concise and hands-on lectures.

Ответы [ 3 ]

1 голос
/ 25 сентября 2019

I need to remove those or replace the with the tag text. В вашем примере я вижу, что вам нужно просто удалить якорные теги и иметь только простой текст.

Только из вашего примера есть простое решение.

var test = document.getElementById("test")
test.innerHTML = test.innerText
<div id="test">
  <a href="#">Some Link</a>
  Plain text
</div>

Но если вам нужно более сложное решение для выбора конкретного links и полного удаления некоторых из них, а другого - просто текста, для этого потребуетсяБолее конкретное описание от вас.

0 голосов
/ 25 сентября 2019

var str = 'Entity Framework is an <a href="http://www.google.com">Object </a>/ Relational Mapper (O/RM) that helps you read and write data from and to a database. In this course, Mosh, teaches you the core concepts of Entity <a href="http://www.google.com">Framework</a>through a series of clear, concise and hands-on lectures.'

var str1 = str.replace(/<\/?[^>]+(>|$)/g, "");

console.log(str1)
0 голосов
/ 25 сентября 2019

В JavaScript вы можете использовать функцию replace(..) с regx:

EX:

var text ='Entity Framework is an <a href="http://www.google.com">Object </a>/ Relational Mapper (O/RM) that helps you read and write data from and to a database. In this course, Mosh, teaches you the core concepts of Entity <a href="http://www.google.com">Framework</a>through a series of clear, concise and hands-on lectures.';

text.replace(/<[^>]*>/g, '');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...