Dave
Dave

Reputation: 11

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

I want to do the following help me making this script:

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")

Please note: I dont want to touch the contained string only the part of it where it is "ABC" and or delete this part only.

I hope you can understand.

I need this script to work under iMacros (web automation software)

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

Upvotes: 0

Views: 115

Answers (3)

Pugazh
Pugazh

Reputation: 9561

Use replace() to remove a specific text and use concat() to add text.

var str = "Whatever words ABC";
var newStr = "";

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

console.log(newStr);

Upvotes: 0

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

try this:

    var str = "Whatever words ABC";

    if (str.indexOf('ABC') !== -1)
      str.replace('ABC', '')
    else
      str+= ' ABC'

Upvotes: 1

Ruhul
Ruhul

Reputation: 1030

Try this

var str ="ABCDEFGHI"


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

Upvotes: 0

Related Questions