使用 Javascript/jQuery 将第 n 个制表符替换为字符串
Replace n-th tab character with a string using Javascript/jQuery
我在 Excel 中有一行项目,当我将它们复制到文本区域时,我有几个制表符。我是故意用这个的。有没有办法用新行(每个)替换第 n 个制表符并为每个制表符插入一个特定的词?这是我目前所拥有的 http://jsfiddle.net/kjuxk7m9/
<textarea rows="10" cols="50" onblur="this.value=this.value.replace(/\t/g, '\n')">
基本上,当我在文本区域中输入以下内容时:
Item1 Item2 Item3 Item4 Item5
(项目之间是制表符),我的结果必须是:
Name1: Item1
Name2: Item2
Name3: Item3
Name4: Item4
Name5: Item5
另外,我可能有一个接一个的制表符。我需要将它们删除,以便在用换行符替换它们后没有空行。此外,我宁愿不使用内联 javascript,即使我在示例中到目前为止已经这样做了。
谢谢!
更新:
输入文字:
Item1 Item2 Item3 Item4 item6 Item7
预期结果是:
LastName: Item1
FirstName: Item2
PhoneNumber: Item3
Nickname: Item4
AnotherRandomCategory:
Blog: Item6
OtherDetail: Item7
Item5 实际上是我的电子表格中空单元格的示例,复制它会添加一个额外的制表符。获得上述结果后,我可以使用代码删除该行,因为该类别没有项目。 Items 是示例,它们实际上是名称、数字和其他数据。谢谢!
这适用于您的示例:
document.querySelector('textarea').addEventListener('paste', function() {
var self= this;
setTimeout(function() {
self.value =
self.value.split(/\t+/)
.map(function(v, index) {
return 'Name'+(index+1)+': '+v;
})
.join('\r');
},1);
});
需要超时,因为文本区域的值直到粘贴事件后才会更新。
文本 split on the tab character, and that array is updated using map, then join 使用换行符编辑。
正则表达式 /\t+/
导致 split
忽略一行中的多个选项卡。
如果您更喜欢使用模糊事件,则不需要超时:
document.querySelector('textarea').addEventListener('blur', function() {
this.value =
this.value.split(/\t+/)
.map(function(v, index) {
return 'Name'+(index+1)+': '+v;
})
.join('\r');
});
根据讨论更新
下面的代码对此进行了转换:
…进入这个:
document.querySelector('textarea').addEventListener('paste', function() {
var self= this;
setTimeout(function() {
var flds=['LastName',
'FirstName',
'PhoneNumber',
'Nickname',
'AnotherRandomCategory',
'Blog',
'OtherDetail'
],
order=[0, //LastName
1, //FirstName
3, //Nickname
2, //PhoneNumber
4, //AnotherRandomCategory
5, //Blog
6 //OtherDetail
]
self.value =
self.value.split(/\t/)
.map(function(v, index) {
if(v==='' || index>=flds.length) {
return '';
}
else {
return index+' '+flds[index]+': '+v+'\r';
};
})
.sort(function(a,b) {
a= order[a.split(' ')[0]];
b= order[b.split(' ')[0]];
return a-b;
})
.join('')
.replace(/\r\d+ /g,'\r')
.substr(2);
},1);
});
您只需要正则表达式 /([a-z]+(\d+))/gi
我们将根据替换字符串中捕获的组对其进行操作。
我们首先删除所有空格,然后使用上面的正则表达式相应地替换它。
document.querySelector('textarea').addEventListener('blur', function() {
this.value = this.value.replace(/\s+/g,"").replace(/([a-z]+(\d+))/ig, "Name:\n");
});
我在 Excel 中有一行项目,当我将它们复制到文本区域时,我有几个制表符。我是故意用这个的。有没有办法用新行(每个)替换第 n 个制表符并为每个制表符插入一个特定的词?这是我目前所拥有的 http://jsfiddle.net/kjuxk7m9/
<textarea rows="10" cols="50" onblur="this.value=this.value.replace(/\t/g, '\n')">
基本上,当我在文本区域中输入以下内容时:
Item1 Item2 Item3 Item4 Item5
(项目之间是制表符),我的结果必须是:
Name1: Item1
Name2: Item2
Name3: Item3
Name4: Item4
Name5: Item5
另外,我可能有一个接一个的制表符。我需要将它们删除,以便在用换行符替换它们后没有空行。此外,我宁愿不使用内联 javascript,即使我在示例中到目前为止已经这样做了。
谢谢!
更新:
输入文字:
Item1 Item2 Item3 Item4 item6 Item7
预期结果是:
LastName: Item1
FirstName: Item2
PhoneNumber: Item3
Nickname: Item4
AnotherRandomCategory:
Blog: Item6
OtherDetail: Item7
Item5 实际上是我的电子表格中空单元格的示例,复制它会添加一个额外的制表符。获得上述结果后,我可以使用代码删除该行,因为该类别没有项目。 Items 是示例,它们实际上是名称、数字和其他数据。谢谢!
这适用于您的示例:
document.querySelector('textarea').addEventListener('paste', function() {
var self= this;
setTimeout(function() {
self.value =
self.value.split(/\t+/)
.map(function(v, index) {
return 'Name'+(index+1)+': '+v;
})
.join('\r');
},1);
});
需要超时,因为文本区域的值直到粘贴事件后才会更新。
文本 split on the tab character, and that array is updated using map, then join 使用换行符编辑。
正则表达式 /\t+/
导致 split
忽略一行中的多个选项卡。
如果您更喜欢使用模糊事件,则不需要超时:
document.querySelector('textarea').addEventListener('blur', function() {
this.value =
this.value.split(/\t+/)
.map(function(v, index) {
return 'Name'+(index+1)+': '+v;
})
.join('\r');
});
根据讨论更新
下面的代码对此进行了转换:
…进入这个:
document.querySelector('textarea').addEventListener('paste', function() {
var self= this;
setTimeout(function() {
var flds=['LastName',
'FirstName',
'PhoneNumber',
'Nickname',
'AnotherRandomCategory',
'Blog',
'OtherDetail'
],
order=[0, //LastName
1, //FirstName
3, //Nickname
2, //PhoneNumber
4, //AnotherRandomCategory
5, //Blog
6 //OtherDetail
]
self.value =
self.value.split(/\t/)
.map(function(v, index) {
if(v==='' || index>=flds.length) {
return '';
}
else {
return index+' '+flds[index]+': '+v+'\r';
};
})
.sort(function(a,b) {
a= order[a.split(' ')[0]];
b= order[b.split(' ')[0]];
return a-b;
})
.join('')
.replace(/\r\d+ /g,'\r')
.substr(2);
},1);
});
您只需要正则表达式 /([a-z]+(\d+))/gi
我们将根据替换字符串中捕获的组对其进行操作。
我们首先删除所有空格,然后使用上面的正则表达式相应地替换它。
document.querySelector('textarea').addEventListener('blur', function() {
this.value = this.value.replace(/\s+/g,"").replace(/([a-z]+(\d+))/ig, "Name:\n");
});