stringTest.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Created by Tony on 2017/6/21.
  3. */
  4. /**
  5. * Created by Tony on 2017/6/21.
  6. */
  7. var test = require('tape');
  8. test('string test1', function(t){
  9. let str = "at('1.1') + at('1.2') + at('1.3') + at('1.4')";
  10. //let re = /at(/g;
  11. //let re = new RegExp("at\(", "g");
  12. //let str1 = str.replaceAll('re', '@(');
  13. //let str1 = str.replace('at(', '@(');
  14. var str1 = str.split('at1(').join('@(');
  15. //t.equal(str1, "@('1.1') + @('1.2') + @('1.3') + @('1.4')");
  16. t.equal(str1, "at('1.1') + at('1.2') + at('1.3') + at('1.4')");
  17. t.end();
  18. })
  19. test('string test1', function(t){
  20. var str="hello(world)";
  21. var nstr = str.replace(/\([^\)]*\)/g,"");
  22. t.equal(nstr , "hello");
  23. t.end();
  24. })
  25. test('test extract', function(t){
  26. let private_extract_code = function(str, idx){
  27. let rst = '', lBracket = 0, rBracket = 0, firstIdx = idx, lastIdx = 0;
  28. for (let i = idx; i < str.length; i++) {
  29. if (str[i] === '(') {
  30. lBracket++;
  31. if (lBracket == 1) firstIdx = i + 1;
  32. }
  33. if (str[i] === ')') {
  34. rBracket++;
  35. if (lBracket == rBracket) {
  36. lastIdx = i - 1;
  37. if (lastIdx > firstIdx) {
  38. if (str[firstIdx] === "'") firstIdx++;
  39. if (str[lastIdx] !== "'") lastIdx++;
  40. if (lastIdx > firstIdx) {
  41. rst = str.slice(firstIdx, lastIdx);
  42. }
  43. }
  44. break;
  45. }
  46. }
  47. }
  48. return rst;
  49. };
  50. let code = private_extract_code("at('1.1') + at('1.2')", 10);
  51. t.equal(code , "1.2");
  52. t.end();
  53. })