English 中文(简体)
缩略语
原标题:JS replace with regular expression

需要定期表达。 如果我想将“abcdf”改为“PPP”。

XabcdfX is xxxxxxx. <MabcdfM> has xxxxx. abcdf is xxxxxxx. <FabcdfF> zxabcdf abcdf.

然后是预期产出

XPPPX is xxxxxxx. <MabcdfM> has xxxxx. PPP is xxxxxxx. <FabcdfF> zxPPP PPP.

abcdf可以是整个扼杀物,甚至可以是一种扼杀。 这只是字母数字。 只有在不相隔的情况下才能取而代之。

我需要用javascript取而代之。 请帮助我。

问题回答

https://developer.mozilla.org/en/Java/Reference/Global_Objects/String/replace#Specification_a____as_a_paraile”rel=“nofollow” 查询replace:

s = s.replace(/abcdf|(<[^>]*>)/g, function(g0,g1){return g1 ? g1:  PPP ;});

Working Example:

显然,如果你把安格尔的括号nes为外壳,这将失败。

假设你的“主角”不能被推崇,你可以使用头指:

s.replace(/abcdf(?=[^<>]*(<|$))/g, "PPP")

如果你可以肯定,那就没有任何类似和大的;我会利用指数Of-功能,而不是使用一种管理:

index = string.indexOf("<", lastIndex);
index = string.indexOf(">", index);
index = string.indexOf("abcd", index);
string = string.substring(0, index)+"PPP"+string.substring(index+4);
var collect = function (s, re)
{
  var rv = [];
  for (var m; m = re.exec(s); rv.push(m));
  return rv;
}
var s = "XabcdfX is xxxxxxx. <MabcdfM> has xxxxx. abcdf is xxxxxxx. <FabcdfF> zxabcdf abcdf."
var tags = collect(s, /<[^>]*>/g);
var matches = collect(s, /abcdf/g);

console.log("INPUT:", s)

var r = s

MATCH: for (var i = 0, j = 0; i < matches.length; ++i) {
  var m = matches[i]
  var mbeg = m.index
  var mend = mbeg + m[0].length
  TAG: for (; j < tags.length; ++j) {
    var t = tags[j]
    var tbeg = t.index
    var tend = tbeg + t[0].length
    if (tend < mbeg)
      continue TAG
    if (tbeg < mbeg && tend > mend)
      continue MATCH
    if (tbeg > mbeg)
      break TAG
  }
  r = r.substr(0, mbeg) + m[0].toUpperCase() + r.substr(mbeg + m[0].length)
}
console.log("RESULT:", r)




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签