0

我有以下字符串:

var content = '<a href="www.topSecret.com">Link</a>'

我的主要目标是:

var content = '<span>Link</span>'

或者,如果不可能,至少:

var content = '<a href="" >Link</a>'

一些注意事项:

  1. 这个 var 将被发送到 PHP 变量,所以如果使用 PHP 更容易,它也被接受
  2. jQuery 最受欢迎

感谢您的所有帮助!

4

3 回答 3

3

使用 PHP 轻松完成strip_tags()

$str = '<a href="www.topSecret.com">Link</a>';
$result = '<span>' . strip_tags( $str) . '</span>';
echo $result;

输出:

<span>Link</span>
于 2012-11-20T19:12:34.737 回答
2

jQuery版本

var $content = $('<a href="www.topSecret.com">Link</a>');
var result = "<span>" + $content.text() + "</span>";
于 2012-11-20T19:26:09.720 回答
1
  var input = '<a href="www.topSecret.com">Link</a>';  
  pattern = /<a.+>(.*?)<\/a>/;
  matches = input.match(pattern);
  result = '<span>' + matches[1] + '</span>';
  alert(result);

这是工作示例:http: //jsbin.com/oqikib/1/edit

于 2012-11-20T19:20:44.733 回答