Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JavaScript the last word of a string

// Get the last word of a string.

let str = "What I hate most in the world is fanaticism.";

// 1) The slice() method: without Considering the punctuation marks
str.slice(str.lastIndexOf(' ')); // => 'fanaticism.'

// 2) the spit() method():
let arr = str.split(' ');
arr[arr.length - 1]; // => 'fanaticism.'


// Considering the punctuation marks at the end of the string
str.match(/(w+)W*$/)[1]; // => 'fanaticism'
Comment

Js last word in a string

String test =  "This is a sentence";
String lastWord = test.substring(test.lastIndexOf(" ")+1);
Comment

how to get last string in javascript

'abc'.slice(-2);
Comment

get last word in string js

function test(words) {
    var n = words.split(" ");
    return n[n.length - 1];
}
Comment

how to find the last word of a string in javascript

// To get the last "Word" of a string use the following code :

let string = 'I am a string'

let splitString = string.split(' ')

let lastWord = splitString[splitString.length - 1]
console.log(lastWord)

/*
Explanation : Here we just split the string whenever there is a space and we get an array. After that we simply use .length -1 to get the last word contained in that array that is also the last word of the string.
*/
Comment

get last letter of string javascript

// Get last n characters from string
var name = 'Shareek';
var new_str = name.substr(-5); // new_str = 'areek'
Comment

get last character of string javascript

str.charAt(str.length-1) 
Comment

javascript last character of a string

const myString = "linto.yahoo.com.";
const stringLength = myString.length; // this will be 16
console.log('lastChar: ', myString.charAt(stringLength - 1)); // this will be the string
 Run code snippet
Comment

javascript get last word in string

// strips all punctuation and returns the last word of a string
// hyphens (-) aren't stripped, add the hyphen to the regex to strip it as well
function lastWord(words) {
    let n = words.replace(/[[]?.,/#!$%^&*;:{}=|_~()]/g, "").split(" ");
    return n[n.length - 1];
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Error: Expected "payload" to be a plain object. at validate 
Javascript :: javascript change data attribute value 
Javascript :: CocoaPods could not find compatible versions for pod "ReactCommon/jscallinvoker" 
Javascript :: js get meta content 
Javascript :: js cookie 
Javascript :: style display block js 
Javascript :: jetbrains font 
Javascript :: fs node delete folder 
Javascript :: js console log without spaces 
Javascript :: referencing an array value in object key js 
Javascript :: scrool to top jquerry 
Javascript :: scroll to bottom of an element react 
Javascript :: input pattern for np whitespaces at the end or beginning 
Javascript :: javascript leap year 
Javascript :: can i learn backend before frontend 
Javascript :: find object length in javascript 
Javascript :: array traversal backward 
Javascript :: js last element of array 
Javascript :: clear input from file vue 
Javascript :: Concatenating variables and strings in React 
Javascript :: change image src jquery 
Javascript :: stringify 
Javascript :: get current url last part angular 
Javascript :: check if mobile view javascript 
Javascript :: jquery watch checkbox change 
Javascript :: file system replace line js 
Javascript :: node download s3 file 
Javascript :: javascript remove last child element 
Javascript :: character to ascii in js 
Javascript :: toggle multiple classes jquery 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =