var string = "Please click on dashboard and then open the dashboard details to verify your details on the data";
var stringArray = ["dashboard" , "dashboard" , "data"]
var replaceArray = ["https://abcd.com/login" , "https://abcd.com/home" , "https://abcd.com/data"]
for(i=0;i<stringArray.length; i++){
string = string.replace(stringArray[i].trim(), "<a href='"+replaceArray[i].trim()+"'>"+stringArray[i].trim()+"</a>");
}
我有一个字符串和2个阵列。如两个阵列中所述,我需要将字符串替换为各自的锚链接标签。 StringArray定义要链接的单词,并替换应添加URL。就像第一次出现仪表板一样,应与“ https://abcd.com/login”链接,第二次出现“仪表板”的情况应与“ https://abcd.com/home”和“数据”更换。带有“ https://abcd.com/data”。
我试图在字符串中找到单词,并使用repent/replaceAll替换它,对单个出现单词正常工作,但是对于多次出现,它无法正常工作。
任何人都帮助我解决这个问题。
结果:
"Please click on <a href='https://abcd.com/login'><a href='https://abcd.com/home'>dashboard</a></a> and then open the dashboard details to verify your details on the <a href='https://abcd.com/data'>data</a>"
预期输出:
"Please click on <a href='https://abcd.com/login'>dashboard</a> and then open the <a href='https://abcd.com/home'>dashboard</a> details to verify your details on the <a href='https://abcd.com/data'>data</a>"
var string = "Please click on dashboard and then open the dashboard details to verify your details on the data";
var stringArray = ["dashboard" , "dashboard" , "data"]
var replaceArray = ["https://abcd.com/login" , "https://abcd.com/home" , "https://abcd.com/data"]
for(i=0;i<stringArray.length; i++){
string = string.replace(stringArray[i].trim(), "<a href='"+replaceArray[i].trim()+"'>"+stringArray[i].trim()+"</a>");
}
I have a string and 2 arrays like above. I need to replace my string with respective anchor link tags as mentioned in two arrays. stringArray defines the word to be linked and replaceArray defines the URL should be added. Like first occurrence of dashboard should be anchor linked with "https://abcd.com/login" and second occurance of "dashboard" should be replaced with "https://abcd.com/home" and "data" should be replaced with "https://abcd.com/data".
I tried to find out the word in string and replace it using replace/replaceAll, working fine for single occurrence word, but for multiple occurrences it is not working.
Anyone help me to resolve this.
Resulting :
"Please click on <a href='https://abcd.com/login'><a href='https://abcd.com/home'>dashboard</a></a> and then open the dashboard details to verify your details on the <a href='https://abcd.com/data'>data</a>"
Expected Output:
"Please click on <a href='https://abcd.com/login'>dashboard</a> and then open the <a href='https://abcd.com/home'>dashboard</a> details to verify your details on the <a href='https://abcd.com/data'>data</a>"
发布评论
评论(4)
When using a string as the first parameter (substring) to the Javascript
替换
函数,替换
将发现并仅替换第一次出现子字符串。这就是为什么您的“登录”和“ HOME”链接都嵌套在“仪表板”的第一次出现的原因,其余的“仪表板”的出现保持不变。使用是一种解决方案,但是不是唯一的解决方案...使用 a>跟踪最后一个索引,其中
strarray
的单词,然后 - ing字符串,从那里继续进行替换搜索:该解决方案基准 大约比正则表达解决方案以及我在下面发布的减少解决方案。
这是一种将单词并链接到一个数组中的解决方案,然后使用
降低
迭代数组replace_arr
,更新字符串字符串
,并维护匹配索引ii
:与
函数中进行处理,并在内部处理几乎将执行时间切成一半,与将字符串访问到外部到达每次迭代的还原过程:
redaim()
函数中的string
的重构减少方法,以便在...最终解决方案基准的速度几乎是 REGEX解决方案。 :)
When using a string as the first parameter (substring) to the Javascript
replace
function,replace
will find and replace only the first occurrence of the substring. That's why both your "login" and "home" links are nested around the first occurrence of "dashboard", and the remaining occurrences of "dashboard" remain unchanged. Using a regular expression as the first parameter is one solution, however not the only solution...Using
indexOf()
to keep track of the last index where word from arraystrArray
was matched, thenslice
-ing the string after the last insertion to continue the replacement search from there:And this solution benchmarks about 120 times faster than both the regular expression solution, and the reduce solutions I posted below.
Here's a solution combining the words and links into a single array, then using
reduce
to iterate the arrayreplace_arr
, update the stringstring
, and maintain the match indexii
:Refactored reduction method for better performance—initially including
string
in thereduce()
function, and processing internally, cuts execution time almost in half, compared to accessing the string externally to the reduction process with each iteration:...and this final solution benchmarks nearly twice as fast as the regex solution. :)
在这里,使用Regex带有Lookaround的解决方案:
此正则是对被白色空间包围的令牌工作,以避免更换URL内部的文本。
您可以看到有关LookAhead和lookBehind 在这里以及浏览器compatibility 在这里。
Here a solution using a regex with lookaround:
This regex will only work for tokens surrounded by whitespaces to avoid replacing the texts inside URLs.
You can see more about lookahead and lookbehind here and about browser compatibility here.
那一个人呢,
我希望您对此有一个暗示。
它的工作就像魅力一样,将有例外处理供您处理。
How about this one guy,
I hope you get a hint on this one.
It works like a charm, there will be exception handling for you to handle.
提出的方法由
a 地图 ping任务首先合并两个相关数组,搜索术语列表和列表超文本引用,替换项目的另一个数组。
a 代码> 处理替换项目列表并安全的任务(没有遇到相同但已经替换搜索))替换每个搜索的相关完整的超文本表达式。
第二部分可以通过使用
offset
替换
方法。在Offset
值上,当前匹配索引,可以通过编程方式(而降低)将最初提供的字符串值分为已处理和未加工的子字符串/部分。降低
任务的阵列结果获取join
ed返回最终完全替换为字符串值。为此,可以轻松地将上述第一个解决方案重构为新的解决方案,该解决方案可以保持两个折叠方法。
第一个任务是创建一个替换跟踪器,该跟踪器使替换函数能够跟踪每个搜索当前的出现计数和替换。
第二个任务直接做替换 通过
提供的原始字符串
再次仅是正则方法的方法将永远不会像真正跟踪搜索事件的那样可靠(也不那么可读),并且实施了这两个目标一方面是通用的,但也足够专业(只有一定的方法唯一的方法是在早期的限制。后一个术语)。
The presented approach consists of
a
map
ping task which firstly merges two related arrays, the list of search terms and the list of hypertext references, into another array of replacement items.a
reduce
tasks which processes a list of replacement items and safely (without running into the same but already replaced search again) replaces each search by its related complete hypertext expression.The 2nd part gets achieved by making use of the
offset
parameter of thereplace
method'sreplacerFunction
. Upon theoffset
value, the current matches index, one can programmatically (while reducing) split the originally provided string value into processed and unprocessed substrings/partials. Thereduce
task's array result getsjoin
ed back into the final entirely replaced string value.For this the above presented first solution could be easily refactored into a new one which keeps the two folded approach.
The first task would be the creation of a replacement tracker which enables a replacer function to keep track of each search's current occurrence count and replacement.
The second task straightforwardly does
replace
the provided original string viaAgain a regex only approach will never be as reliable (also not as readable) as one that truly tracks search occurrences and was implemented with both goals being generic on one hand but also specialized enough (a regex only approach is limited much earlier in the latter terms).