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

get last word in string js

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

how to get last string in javascript

'abc'.slice(-2);
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 :: How do I push an element into the array within an object in AngularJS 
Javascript :: flatlist react native 
Javascript :: how to send static file in express 
Javascript :: how to change the tab text in React 
Javascript :: get cookie javascript 
Javascript :: how to show bootstrap 5 modal using jquery 
Javascript :: date split in javascript 
Javascript :: how to generate color code from random number 
Javascript :: react hooks delete item from array 
Javascript :: find in array react 
Javascript :: You need to authorize this machine using `npm adduser` 
Javascript :: array contains multiple js 
Javascript :: How to Loop Through an Array with a for…in Loop in JavaScript 
Javascript :: Iterate with JavaScript For Loops 
Javascript :: wordpress ajax url 
Javascript :: roblox headshot image js 
Javascript :: javascript remove empty object items 
Javascript :: jquery remove attribute 
Javascript :: clear input field react-hook-form 
Javascript :: react native elevation 
Javascript :: content editable vuejs 
Javascript :: regex any char except 
Javascript :: Get React Native View width and height 
Javascript :: javascript replace all 
Javascript :: check if input is a number javascript 
Javascript :: button size react native 
Javascript :: merge two objects javascript 
Javascript :: why do you have to set key prop in react 
Javascript :: request body empty express 
Javascript :: how to make a 2 value after point javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =