Search code examples
javascriptimacros

How to remove random part of string in javascript imacros


I have some strings

"D:\trial\img\2\5-trendy-high-heel-oxford-shoes.jpg"

"D:\trial\img\3\20-cute-heels-street-style_01.jpg"

"D:\trial\img\4\637d3c5673d97326f9517ad24ba99e76.jpg"

"D:\trial\img\5\126003_P_1388122701631.jpg"

I wants to remove "D:\trial\img\2\" and wants only "5-trendy-high-heel-oxford-shoes.jpg"

But as you can see all string have different number after sub string img.

Please help me to fix this for imacros javascript eval feature.

here is my imacros code

SET !DATASOURCE D:\trial\ima_kuwot.csv
SET !DATASOURCE_COLUMNS 50
SET !LOOP 2
SET !DATASOURCE_LINE {{!LOOP}}
SET !VAR1 {{!COL3}}
Add !EXTRACT "D:\trial\img\3\20-cute-heels-street-style_01.jpg"
SET !VAR1 EVAL("var s=\"{{!EXTRACT}}\"; s.split('img\')[2];")
SET !VAR1 EVAL("var s=\"{{!EXTRACT}}\"; s.replace(/[^0-9\.]+/g, "");") 
SET !VAR2 EVAL("var s=\"{{!VAR1}}\"; s.replace(/[]+/g,'.jpg')")
SET !VAR2 EVAL("'{{!VAR1}}'.split('D:"\trial\img\1\"')[0].trim();")
PROMPT {{!VAR1}}
TAG POS=1 TYPE=INPUT:SEARCH ATTR=ID:media-search-input CONTENT={{!VAR1}}

I tried above eval statements but nothing working for me.


Solution

  • Fortunately, the following syntax works:

    SET !EXTRACT D:\\\trial\\\img\\\3\\\5-trendy-high-heel-oxford-shoes.jpg
    SET !VAR1 EVAL("'{{!EXTRACT}}'.match(/[\d\w\-\.]+$/);")
    PROMPT {{!VAR1}}
    

    (At least in 'iMacros for Firefox' and 'iMacros for Chrome'.)