JavaScript 拆分,按最后一个点“.”拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29825464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript Split, Split string by last DOT "."
提问by Mukesh Kumar
JavaScript Split,
JavaScript 拆分,
str = '123.2345.34' ,
expected output 123.2345 and 34Str = 123,23.34.23
expected output 123,23.34 and 23
str = '123.2345.34' ,
预期输出 123.2345 和 34Str = 123,23.34.23
预期输出 123,23.34 和 23
Goal : JS function to Split a string based on dot(from last) in O(n). There may be n number of ,.(commas or dots) in string.
目标:JS 函数根据 O(n) 中的点(从最后一个)拆分字符串。字符串中可能有 n 个 ,.(逗号或点)。
回答by Albin
In order to splita string matching only the last character like described you need to use regex "lookahead".
为了split只匹配最后一个字符的字符串,您需要使用正则表达式“lookahead”。
This simple example works for your case:
这个简单的例子适用于你的情况:
var array = '123.2345.34'.split(/\.(?=[^\.]+$)/);
console.log(array);
Example with destructuring assignment (Ecmascript 2015)
解构赋值示例(Ecmascript 2015)
const input = 'jquery.somePlugin.v1.6.3.js';
const [pluginName, fileExtension] = input.split(/\.(?=[^\.]+$)/);
console.log(pluginName, fileExtension);
However using either sliceor substringwith lastIndexOfalso works, and albeit less elegant it's much faster:
然而,使用sliceor 或substringwithlastIndexOf也可以,虽然不那么优雅,但速度要快得多:
var input = 'jquery.somePlugin.v1.6.3.js';
var period = input.lastIndexOf('.');
var pluginName = input.substring(0, period);
var fileExtension = input.substring(period + 1);
console.log(pluginName, fileExtension);
回答by Mritunjay
I will try something like bellow
我会尝试像下面这样的东西
var splitByLastDot = function(text) {
var index = text.lastIndexOf('.');
return [text.slice(0, index), text.slice(index + 1)]
}
console.log(splitByLastDot('123.2345.34'))
console.log(splitByLastDot('123,23.34.23'))
回答by Rahaman
var arr = str.split("."); // Split the string using dot as separator
var lastVal = arr.pop(); // Get last element
var firstVal = arr.join("."); // Re-join the remaining substrings, using dot as separatos
console.log(firstVal +" and "+lastVal); //Printing result
回答by jcubic
I came up with this:
我想出了这个:
var str = '123,23.34.23';
var result = str.replace(/\.([^.]+)$/, ':').split(':');
document.getElementById('output').innerHTML = JSON.stringify(result);
<div id="output"></div>
回答by Tushar
Try this:
尝试这个:
var str = '123.2345.34',
arr = str.split('.'),
output = arr.pop();
str = arr.join('.');
回答by S. Domeng
I'm typically using this code and this works fine for me.
我通常使用此代码,这对我来说很好用。
Jquery:
查询:
var afterDot = value.substr(value.lastIndexOf('_') + 1);
console.log(afterDot);
Javascript:
Javascript:
var myString = 'asd/f/df/xc/asd/test.jpg'
var parts = myString.split('/');
var answer = parts[parts.length - 1];
console.log(answer);
Note: Replace quoted string to your own need
注意:根据自己的需要替换带引号的字符串
回答by prashanth-g
var test = 'filename.....png';
var lastStr = test.lastIndexOf(".");
var str = test.substring(lastStr + 1);
console.log(str);
回答by Penny Liu
You can use lodash _.toPathfirst, then you just convert all elements in array into a string separated by separator.
您可以先使用 lodash _.toPath,然后您只需将数组中的所有元素转换为由分隔符分隔的字符串。
let str = '123,23.34.23';
// _.toPath(str)
// => ["123,23", "34", "23"]
let str1 = _.toPath(str);
let str2 = str1.pop();
let [major, minor] = [_.join(str1, '.'), str2];
console.log([major, minor]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
回答by Akash gupta
The simplest way is mentioned below, you will get pdfas the output:
下面提到了最简单的方法,您将获得pdf作为输出:
var str = "http://somedomain.com/dir/sd/test.pdf";
var ext = str.split('.')[str.split('.').length-1];
var str = "http://somedomain.com/dir/sd/test.pdf";
var ext = str.split('.')[str.split('.').length-1];
Output: pdf
输出:pdf
回答by Nayereh
let returnFileIndex = str =>
str.split('.').pop();

