从POST响应中删除评论标记

时间:2013-10-27 13:31:59

标签: javascript jquery regex

我正在使用JQuery并尝试发布帖子请求,而我得到的响应是

类型
/*this is the string i need*/

现在我正在尝试访问我需要的字符串,但是对于某些范围问题,我似乎没有做对。

我的代码是:

$.get( "/geturl", function( data ) {
        console.log(hereStr(data)); ==> prints /*this is the string i need*/
});


function hereStr(f) {
  return f.
      replace(/^[^\/]+\/\*!?/, '').
      replace(/\*\/[^\/]+$/, '');
}

但我的console.log只打印带有注释标记的相同字符串,如上所示 我的正则表达式错了吗?或者这是一些范围问题?

任何指针都会非常感激 感谢

1 个答案:

答案 0 :(得分:2)

hereStr()功能的更正版本:

function hereStr(f) {
  return f.replace(/\/\*(.+)\*\//, '$1');
}