How to trim to the first character, for example:

var str="12323{123123}{sdfsdfsdf}"; 

trim result:

 var str1="12323"; var str2="{123123}{sdfsdfsdf}"; 

while before the symbol {value may be of different lengths. Any ideas? thank you in advance!

  • one
    str.indexOf("{"); , then cut the string to this index, and after, well, or how you will need it later. - Moonvvell
  • one
    so you need to cut, or divide the line by 2? or a few? - Grundy
  • create a regular season only for letters, cut off on the first mismatch - lexxl

5 answers 5

 var str="12323{123123}{sdfsdfsdf}"; var pos = str.indexOf('{'); // 5 var s1 = str.substr(0,pos); // 12323 var s2 = str.substr(pos); // {123123}{sdfsdfsdf} 
     var str="12323{123123}{sdfsdfsdf}"; var strSplit = str.split('{'); var resultStr= str.substr(strSplit[0].length); 
       var str="12323{123123}{sdfsdfsdf}"; var str1 = str.match(/[a-z0-9]+/i)[0]; // выбор всех букв+цифр (не символов) в первую переменную var str2 = str.substring(str.match(/[^a-z0-9]/i).index); // выбор всего, что после первого символа во вторую переменную 

        Another option with split

         var str="12323{123123}{sdfsdfsdf}"; var split = str.split(/({)/); var str1 = split.splice(0,1); var str2 = split.join(''); document.write('str: '+str+'<br/>'); document.write('str1: '+str1+'<br/>'); document.write('str2: '+str2+'<br/>'); 

           var str="12323{123123}{sdfsdfsdf}"; res = str.match(/([^{]+)({.*)/) str1 = res[1] str2 = res[2]