lines[i].trim(); does NOT modify the current string (see the doc here). It returns a new string.
If you want to trim the current string, then you need to do this:
lines[i] = lines[i].trim();
Answer from jfriend00 on Stack Overflowlines[i].trim(); does NOT modify the current string (see the doc here). It returns a new string.
If you want to trim the current string, then you need to do this:
lines[i] = lines[i].trim();
As per comments, here's very basic version of pig latin using regex that works with lowercase strings but it can be adjusted to handle mixed lower and upper case:
function pigLatin(str) {
return str
.toLowerCase() // make sure string is lowercase
.replace(/\b[aeiuo]\w+\b/g, '$&way') // starts with vowel
.replace(/\b([^aeiou\s])(\w+)\b/g, '
1ay'); // starts with consonant
}
// Example:
var str = 'hello world egg plant yellow';
console.log(pigLatin(str)); //=> "ellohay orldway eggway lantpay ellowyay"
jquery - javascript trim() doesn't work - Stack Overflow
javascript - Why is the trim() method not working but instead breaking my function? - Stack Overflow
internet explorer - .trim() in JavaScript not working in IE - Stack Overflow
.trim() in JavaScript not working in IE8
Try mystring.replace(/\s+/g, ' '); and $.trim()
As Pavel and Arun P pointed in the comment, it is a possible duplicate question.
As you can read in the documentation of the trim() function, this function removes whitespaces before and after the string but it won't touch the whitespaces in the string.
If you want to remove the whitespaces in the string too, you have to write your own function.
Here a little example:
function cleanStr(str) {
while (str.indexOf("\t") > -1) {
str = str.replace("\t", " ");
}
while (str.indexOf(" ") > -1) {
str = str.replace(" ", " ");
}
return str;
}
Example of use:
cleanStr(" aasdfqwer wer asdaf awer 5asadf sdf ");
Result:
" aasdfqwer wer asdaf awer 5asadf sdf "
Add the following code to add trim functionality to the string.
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
It looks like that function isn't implemented in IE. If you're using jQuery, you could use $.trim() instead (although is has been deprecated as of jQuery 3.5).
jQuery has a trim function that can be used like:
var trimmedValue = $.trim(inputNameVal);
Also, inputNameVal.trim() is a function call that does not modify the value of the string, you'd have to have something like:
inputNameVal = inputNameVal.trim()
if String.trim is not defined for your browser mozilla documentation says you can define it yourself like:
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
String.trim() is not known by all browsers (see at the bottom of the page).
As you tagged this question with jQuery, you can use $.trim() like this :
$.trim(inputNameVal)
You should assign it back otherwise it's meaningless.
txt = txt.trim();
input = $("<input type='text'").val(txt);
Note: the above is using the native JavaScript trim() method, assuming txt is plain string. If your site should support IE8 and below, refer to this answer to see how to add such support.
Update, based on OP comments. Turns out the request is to truncate all inner spaces, same way HTML is doing. To achieve this, plus better support for the trim change this line:
var text = $(this).text();
To those three:
var text = $.trim($(this).text());
while (text.indexOf(" ") > 0)
text = text.replace(" ", " ");
Updated fiddle.
Apply trim() to string i.e. txt but not to jQuery object (textbox control)
input = $("<input type='text'").val($.trim(txt));