如何用iMacros下的条件替换字符串的特定部分

How to replace only specific part of the string with condition under iMacros

本文关键字:字符串 定部 替换 条件 何用 iMacros      更新时间:2023-09-26

我想做以下帮助我制作这个脚本:

if form text include = "ABC" (for example: "Whatever words ABC")
then delete the word "ABC" (so it become "Whatever words")
AND
if form text doesn't include = "ABC"
then add the word at the end "ABC (so it become "Whatever words ABC")

请注意:我不想触摸所包含的字符串,只有它的一部分,它是"ABC"或删除这部分。

希望你能理解。

我需要这个脚本在iMacros (web自动化软件)下工作

SOLVED: SET !VAR2 EVAL("'{{!EXTRACT}}'.includes('ABC') ? '{{!EXTRACT}}'.replace('ABC','') : '{{!EXTRACT}} ABC';")

try this:

    var str = "Whatever words ABC";
    if (str.indexOf('ABC') !== -1)
      str.replace('ABC', '')
    else
      str+= ' ABC'

试试这个

var str ="ABCDEFGHI"

if(str.indexOf("ABC") !== -1){
str.replace(/ABC/g,"")
}else{
str=str+"ABC"
}

使用replace()删除特定文本,使用concat()添加文本。

var str = "Whatever words ABC";
var newStr = "";
if (str.indexOf("ABC") > -1) {
  newStr = str.replace(" ABC", "");
} else {
  newStr = str.concat(" ABC");
}
console.log(newStr);