var a = "qwertyqwerBtyA", b = "qwertyqwerBt\nyA", b_reg = /B.*A|A.*В/gim, a_reg = /B[\S\s]*A|A[\S\s]*В/gim; alert([b_reg.test(a),b_reg.test(b),a_reg.test(b)]);