简体   繁体   English

使用Java脚本:如何更改内容替换功能以获得所需的结果?

[英]Using Java script :How can I change my content replace function to get my desire result?

When you copy and paste 当您复制并粘贴时

<tt>1<tt>2<tt>3<tt>

into this function: 进入此功能:

function process() {
var content=
document.getElementById('content').value;

content= content. replace(/<tt>)(.*?)(<tt>(.*?)<tt>(.*?)<tt>/g, '<$2><$2><$2>');

document.getElementById('content').value=content;
}

HTML 的HTML

<textarea id="content" cols="48" rows="8">
</textarea><br/>
<input type="button" value="Process"
onclick="process()"/>

And click process you will yield this result: 然后单击过程,您将得到以下结果:

<1><1><1>

How can I yield this result instead? 我怎样才能得到这个结果呢?

<1><2><1>

I can not simply use this content.replace 我不能简单地使用此内容。

content= content. replace(/<tt>)(.*?)(<tt>(.*?)<tt>(.*?)<tt>/g, '<$2><2><$2>');

to yield my desired result since the numbers are subject to change: for instance one day they may be 产生我想要的结果,因为数字可能会发生变化:例如,有一天可能

<tt>2<tt>5648<tt>19897<tt> 

which would currently yield this result: 当前会产生以下结果:

<2><2><2>

while I would prefer to have it yield this result: 虽然我希望它产生以下结果:

<2><5648><2>

to summarize my question,how can I have the first and third number both change to the 1st number whilst the second number remains constant throughout. 总结一下我的问题,我如何才能将第一个和第三个数字都更改为第一个数字,而第二个数字始终保持不变。 I will provide a link to my example currently set up http://jsfiddle.net/WVUYX/35/ 我将提供指向我当前设置的示例的链接http://jsfiddle.net/WVUYX/35/

尝试这个:

.replace(/<tt>(\d+)<tt>(\d+)<tt>\d+<tt>/,"<$1><$2><$1>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM