18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to use regular expression in a script

ShwethaShwetha Members
edited November -1 in Sahi - Open Source
Hi Narayan,

Please let me know how to use regular expression for this case:

_click(_link("linkname[2]"));

The number in the square braces is changing sometimes it 1 or 2 or 3.

Actually ' linkname ' is the name of the link which I have to click, in some places of application it is concatenated with number like [1] or [2] or [3].

so I should send the link name in a variable and concate with this changing number,

var $LINKName = " linkname ";

var $linkToClick = $LINKName +"["+[0-9]];
>I have tried this expression,but its not working

_click(_link($linkToClick));

Comments

  • narayannarayan Administrators
    Shweta,

    Is the link itself named linkname[2] in your application or is Sahi identifying it as linkname[2] while the link is just linkname?
    If it is the first case, you can use
    _click(_link(/linkname\[[0-9]\]/));
    // Note the use of forward slashes to start and end regular expressions in javascript.
    
    If you want to concatenate a string and then do the same thing
    var $LINKName = " linkname";
    var $linkToClick = $LINKName +"\\[[0-9]\\]";
    _click(_link(new RegExp($linkToClick)));
    // Note the use of new RegExp(str) to convert a string to a regular expression.
    
    Regards,
    Narayan
  • Narayan,
    Thank You. But the link does not appear like that. SAHI is recognizing the link as _link("_linkname[1]") with the no. being different every time. I tried the above suggested two methods. But it is not recognized. Please help.
  • madhumadhu Members
    Hi Narayan,
    I could you please let me know how to use the regular expression in SAHI

    var $sAcctType = "*Savings*" //The constant value is "Savings" at run time it was " 12312312 Savings"
    _setSelected(_select("listAccounts"), $sAcctType );

    Above statement doesnt work
    Thanks in advance
  • narayannarayan Administrators
    Madhu,

    Seems like there is a bug in Sahi which is preventing use of RegExp in _setSelected.
    To fix it,

    Open sahi/htdocs/spr/concat.js

    Look for
    Sahi.prototype._setSelected
    
    3 lines below that you will see
    if (typeof val == "string"){
    

    Change it to
    if (typeof val == "string" || val instanceof RegExp){
    

    Restart Sahi, and clear your browser cache or force refresh the page.

    Then you can use:
    _setSelected(_select("listAccounts"), /.*Savings/);
    

    I will add the fix in the next build. Thanks for pointing it out.

    Regards,
    Narayan
  • madhumadhu Members
    It works great thanks Narayan [:)].
This discussion has been closed.