Mukesh Kumar
Mukesh Kumar

Reputation: 341

JavaScript Split, Split string by last DOT "."

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.

  1. str = '123.2345.34' expected output 123.2345 and 34

  2. Str = '123,23.34.23' expected output 123,23.34 and 23

Upvotes: 18

Views: 53041

Answers (14)

j08691
j08691

Reputation: 207901

By using the new flat() method with join() and pop() you can do:

Example 1

// expected output 123.2345 and 34
var str = '123.2345.34';
var result = str.split('.');
var last = result.pop();
var rest = result.flat().join('.');
console.log(rest, last);

Example 2

// expected output 123,23.34 and 23
var str = '123,23.34.23';
var result = str.split('.');
var last = result.pop();
var rest = result.flat().join('.');
console.log(rest, last);

Upvotes: 0

i100
i100

Reputation: 4666

very very old question, but I believe it always worth another answer ;-)

const s = '123.2345.123456.34';

function splitString(t) {
  let a = t.split(/\./);
  return [a.slice(0, -1).join('.'), a.pop()];
}

const [first, last] = splitString(s);

console.log(`first: ${first}, last: ${last}`);

Upvotes: 0

Rahaman
Rahaman

Reputation: 333

var str = "filename.to.split.pdf"
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 separator

console.log(firstVal + " and " + lastVal);  //Printing result

Upvotes: 16

Nayereh
Nayereh

Reputation: 51

let returnFileIndex = str =>
    str.split('.').pop();

Upvotes: 5

Akash gupta
Akash gupta

Reputation: 336

The simplest way is mentioned below, you will get pdf as the output:

var str = "http://somedomain.com/dir/sd/test.pdf"; var ext = str.split('.')[str.split('.').length-1];

Output: pdf

Upvotes: -1

SMPLYJR
SMPLYJR

Reputation: 884

I'm typically using this code and this works fine for me.

Jquery:

var afterDot = value.substr(value.lastIndexOf('_') + 1);
console.log(afterDot);

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

Upvotes: 1

Albin
Albin

Reputation: 2558

In order to split a string matching only the last character like described you need to use regex "lookahead".

This simple example works for your case:

var array = '123.2345.34'.split(/\.(?=[^\.]+$)/);
console.log(array);

Example with destructuring assignment (Ecmascript 2015)

const input = 'jquery.somePlugin.v1.6.3.js';
const [pluginName, fileExtension] = input.split(/\.(?=[^\.]+$)/);
console.log(pluginName, fileExtension);

However using either slice or substring with lastIndexOf also works, and albeit less elegant it's much faster:

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);

Upvotes: 44

prashanth-g
prashanth-g

Reputation: 1233

var test = 'filename.....png';
var lastStr = test.lastIndexOf(".");
var str = test.substring(lastStr + 1);
console.log(str);

Upvotes: 1

Nandan Bhat
Nandan Bhat

Reputation: 1563

Try this solution. Simple Spilt logic

 <script type="text/javascript">

 var str = "123,23.34.23";
 var str_array = str.split(".");
 for (var i=0;i<str_array.length;i++)
 {
     if (i == (str_array.length-1))
     {
         alert(str_array[i]);
     }
 }
 </script>

Upvotes: 0

freethinker
freethinker

Reputation: 2425

Str = '123,23.34.23';

var a = Str.substring(0, Str.lastIndexOf(".")) //123,23.34 
var b = Str.substring(Str.lastIndexOf(".")) //23

Upvotes: 0

Hackerman
Hackerman

Reputation: 12305

My own version:

var mySplit;
var str1;
var str2;
$(function(){
  mySplit = function(myString){
    var lastPoint = myString.lastIndexOf(".");
    str1 = myString.substring(0, lastPoint);
    str2 = myString.substring(lastPoint + 1);
  }
  mySplit('123,23.34.23');
  console.log(str1);
  console.log(str2);
});

Working fiddle: https://jsfiddle.net/robertrozas/no01uya0/

Upvotes: 0

Mritunjay
Mritunjay

Reputation: 25882

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'))

Upvotes: 11

jcubic
jcubic

Reputation: 66488

I came up with this:

var str = '123,23.34.23';
var result = str.replace(/\.([^.]+)$/, ':$1').split(':');
document.getElementById('output').innerHTML = JSON.stringify(result);
<div id="output"></div>

Upvotes: 5

Tushar
Tushar

Reputation: 87203

Try this:

var str = '123.2345.34',
    arr = str.split('.'),
    output = arr.pop();
str = arr.join('.');

Upvotes: 1

Related Questions