0

I've different results if I use a RegExp pattern and when I use a new RegExp pattern... I'm a noob with RegExp. So

import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.text.TextField;
import flash.ui.ContextMenu;

    var myString1:String = "Sharsks in the sea";
    var pattern1:RegExp = new RegExp("^\s*|\s*$","gim");
    var pattern2:RegExp = new RegExp("\s*|\s*$","gim");
    var pTest:RegExp = /\s*|\s*$/gim;
    var result1:String = myString1.replace(/^\s*|\s*$/gim,"_");
    var result2:String = myString1.replace(pattern1,"_");
    var result3:String = myString1.replace(/\s*|\s*$/gim,"_");
    var result4:String = myString1.replace(pattern2,"_");
    var result5:String = myString1.replace(pTest,"_");

    trace(result1);
    trace(result2);
    trace(result3);
    trace(result4);
    trace(result5);

    outputs : 
    _Sharsks in the sea_
    _harsks in the sea_
    _S_h_a_r_s_k_s__i_n__t_h_e__s_e_a_
    __h_a_r__k__ _i_n_ _t_h_e_ __e_a_
    _S_h_a_r_s_k_s__i_n__t_h_e__s_e_a_

Can anyone tell me the difference between new operator and a logical RegExp pattern.

I know that is a very stupid question, so pardon me, but I'm confused...

tatactic
  • 1,379
  • 1
  • 9
  • 18

1 Answers1

1

The RegExp class lets you work with regular expressions, which are patterns that you can use to perform searches in strings and to replace text in strings. You can create a new RegExp object by using the new RegExp() constructor or by assigning a RegExp literal to a variable:

var pattern1:RegExp = new RegExp("test-\\d", "i");
var pattern2:RegExp = /test-\d/i;

From adobe

In other words, they are equivalent.

Neal Davis
  • 2,010
  • 2
  • 12
  • 25
  • Thank you so much @NealDavis (upvoted) but why did I get a different output with trace(result4); and trace(result5); ? Is there a way to figure me that easily? I will try this tomorrow! Perhaps it will make sense immediately. (I hope it) ;) – tatactic Oct 23 '16 at 19:58
  • 1
    I think you may need to put a forward slash in front of the second back slash in the second one. – Neal Davis Oct 24 '16 at 02:23