i want to replace the "." into tokens like [dot] from below input to below output
我想把“。”替换成从下面输入到输出下面的令牌。
input
输入
this is a test.for a question. at *.com the best place to learn. programming. test www.wikipedia.com
这是一个测试。的一个问题。在*.com上学习的最佳地点。编程。测试www.wikipedia.com
output
输出
this is a test.for a question. at *[dot]com the best place to learn. programming. test www[dot]wikipedia[dot]com
这是一个测试。的一个问题。在*[dot]com网站是最好的学习场所。编程。测试www[点]*点com
problems
问题
- there is a possibly
test.for
we cant use good regex like/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}/gi
i think its best for using something like below better - 有一个可能的测试。我们不能用很好的正则表达式/(a-z0-9)+((\ \]{ 1 }[a-z0-9]+)* \。[a-z]{2,5}/gi我认为使用下面这样的东西最好
- maybe i found a solution;
- 也许我找到了解决办法;
found = string.match(/([a-zA-Z0-9]+\.(com|co\.cc)|more\.domains)/gi);
发现= string.match(/((a-zA-Z0-9)+ \。(com |有限公司\ .cc)|更多\ .domains)/ gi);
this work great, i have a problem to join/replace them to the original string. any workarounds like how can we filter elements in array with regex in array with javascript?
这工作很好,我有一个问题连接/替换它们到原始字符串。任何解决方法,比如我们如何使用javascript在数组中使用regex过滤数组中的元素?
how would you tackle this problem? btw im using nodejs other language is acceptable.
你将如何解决这个问题?顺便说一句,我使用nodejs其他语言是可以接受的。
thanks
谢谢
2 个解决方案
#1
3
This handles www.example.com
correctly:
这个处理www.example.com正确:
tld = ["com", "org", "edu", "net"] // feel free to add more
var input = "this is a test.for a question. at www.*.com "
+ "the best place to learn. "
+ "programming.test wikipedia.com and windows.microsoft.edu";
re = new RegExp('\\S+\\.(' + tld.join('|') + ')\\b', 'g')
var dotted = input.replace(re, function($0) {
return $0.replace(/\./g, "[dot]");
});
// this is a test.for a question. at www[dot]*[dot]com the best place to learn.
// programming.test wikipedia[dot]com and windows[dot]microsoft[dot]edu
#2
1
var input = "this is a test.for a question. at *.com the best place to learn. programming. test wikipedia.com";
var dotted = input.replace(/(\S+)\.(com|org|edu|net)\b/gi, '$1[dot]$2');
// "this is a test.for a question. at *[dot]com the best place to learn. programming. test wikipedia[dot]com"
#1
3
This handles www.example.com
correctly:
这个处理www.example.com正确:
tld = ["com", "org", "edu", "net"] // feel free to add more
var input = "this is a test.for a question. at www.*.com "
+ "the best place to learn. "
+ "programming.test wikipedia.com and windows.microsoft.edu";
re = new RegExp('\\S+\\.(' + tld.join('|') + ')\\b', 'g')
var dotted = input.replace(re, function($0) {
return $0.replace(/\./g, "[dot]");
});
// this is a test.for a question. at www[dot]*[dot]com the best place to learn.
// programming.test wikipedia[dot]com and windows[dot]microsoft[dot]edu
#2
1
var input = "this is a test.for a question. at *.com the best place to learn. programming. test wikipedia.com";
var dotted = input.replace(/(\S+)\.(com|org|edu|net)\b/gi, '$1[dot]$2');
// "this is a test.for a question. at *[dot]com the best place to learn. programming. test wikipedia[dot]com"