Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

javascript - Javascript溢出功能无法识别字符串中的选项卡之一(Javascript spilt function does not recognise one of the tabs in my string)

I have a string with tab-separated data copied from Excel.(我有一个字符串,其中包含从Excel复制的制表符分隔的数据。)

The javascript split does not recognise the 2nd tab in the string.(javascript拆分无法识别字符串中的第二个标签。) I have pasted the string into notepad++ to see the tabs and they are all there.(我已将字符串粘贴到notepad ++中,以查看选项卡,它们都在那里。) Exploding the string in PHP works fine.(在PHP中爆炸字符串可以正常工作。) The test code is:(测试代码为:)
    function testTab(str) {
        var strSplit = str.split('');
        for (i=0; i < strSplit.length; i++){
            console.log('strSplit['+i+'] = '+strSplit[i]);
        }
    }

The console output (where the tab between 1st and 2nd item is not recognised):(控制台输出(其中无法识别第一项和第二项之间的选项卡):)

strSplit[0] = 0.02194 0.028940568
strSplit[1] = 0.05227
strSplit[2] = 0.040229885
strSplit[3] = 0.04650
strSplit[4] = 0.035630689
strSplit[5] = 0.07055
strSplit[6] = 0.015557256
strSplit[7] = 0.01960
strSplit[8] = 0.03527
strSplit[9] = 0.05276
strSplit[10] = 0.05669
strSplit[11] = 0.05680
strSplit[12] = 0.04464
strSplit[13] = 1

Unsure if the string copies correctly with all tabs, but here it is:(不确定该字符串是否正确复制了所有选项卡,但是这里是:)

 const str = `0.02194 0.028940568 0.05227 0.040229885 0.04650 0.035630689 0.07055 0.015557256 0.01960 0.03527 0.05276 0.05669 0.05680 0.04464 1`; function testTab(str) { var strSplit = str.split('\t'); for (i = 0; i < strSplit.length; i++) { console.log('strSplit[' + i + '] = ' + strSplit[i]); } } testTab(str) 

  ask by Svea translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks for your suggestions, it inspired me to review all options.(感谢您的建议,这启发了我复习所有选项。)

I changed the delimiters in the regex so that the split now reads:(我在正则表达式中更改了定界符,以便拆分现在显示为:)

var strSplit = str.split(//);

For some reason it now works.(由于某种原因,它现在可以工作了。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...