admin管理员组文章数量:1401158
I have a string that I have created where I would like to remove the last ma and replace it with a period. I want to keep all of the other mas. Here I was trying to use a conditional statement, it works to add mas, but it doesn't work to replace the last one with a period. I am new at this, I would really appreciate any help.
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
likesString = petObj.name + " " + " is a " + petObj.type + " she " + " likes ";
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length) {
var likesString = likesString + petObj.likes[j] + ", ";
}
else if (j == petObj.likes.length) {
likesString.replace(", ", ".");
}
}
displayResult();
}
I have a string that I have created where I would like to remove the last ma and replace it with a period. I want to keep all of the other mas. Here I was trying to use a conditional statement, it works to add mas, but it doesn't work to replace the last one with a period. I am new at this, I would really appreciate any help.
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
likesString = petObj.name + " " + " is a " + petObj.type + " she " + " likes ";
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length) {
var likesString = likesString + petObj.likes[j] + ", ";
}
else if (j == petObj.likes.length) {
likesString.replace(", ", ".");
}
}
displayResult();
}
Share
Improve this question
edited Dec 28, 2012 at 21:13
David G
97k41 gold badges172 silver badges258 bronze badges
asked Dec 28, 2012 at 21:12
user1876829user1876829
4951 gold badge7 silver badges12 bronze badges
3
- I answered, but then discovered this. Almost an exact duplicate of How to Replace Last... The answers are going to be identical. – bdrelling Commented Dec 28, 2012 at 21:29
- @aerodynamo that article only deals with how to fix up the string after it is created, perhaps by a simple join() call. Part of the problem here is that the condition inside the loop is off-by-one and the condition of the else isn't needed and the result of the 'replace' isn't put back into the string and there is a spare 'var' and if you put the ma in on all the loop iterations, you should put the fixup after the loop ends, not on a condition for the 'last' loop iteration. – Lee Meador Commented Dec 28, 2012 at 21:41
- @LeeMeador Hmm... I get what you're saying, but the very top answer (and most of the answers that follow) show how to replace the last occurrence of a specific pattern with a different pattern. I mean, it's almost entirely identical except it checks for whitespace. If you take out \s and change ' and' to '.', the answer is entirely identical. I would agree that there are other issues with his code, but not directly related to the question he's asking. Thoughts? – bdrelling Commented Dec 29, 2012 at 1:01
7 Answers
Reset to default 3You seem to be doing it a hard way! Use the Arry's join() method to build the list.
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
likesString = petObj.name + " is a " + petObj.type + " she likes " +
petObj.likes.join(", ") + ".";
displayResult();
}
This will do it:
str.replace(/,([^,]*)$/,".$1")
The regular expression matches on a ma followed by any number of non-mas all the way to the end of the string... by definition, this is the last ma. It works if there are no mas, one ma or any number of mas.
Notice that the 'if' part of this has to always be true so the 'else' part is never executed:
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length) {
var likesString = likesString + petObj.likes[j] + ", ";
}
else if (j == petObj.likes.length) {
likesString.replace(", ", ".");
}
}
The for loop says to only continue if that condition is true and your code then tests the same condition.
You might just do something like this:
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length-1) {
likesString = likesString + petObj.likes[j] + ", ";
}
else {
likesString = likesString + petObj.likes[j] + ". ";
}
}
There are better ways to do the condition that don't duplicate so much but that might do what you want. (Also I fixed the extra 'var' part.)
Here is a great place to learn and test regular expressions: RegExr
Essentially, you want to replace the following:
RegExp Pattern: /,([^,]+)$/
Replace Pattern: .$1
So your code should look like:
s.replace(/,([^,]+)$/, '.$1');
Where s
is the string you're trying to replace the last ma in.
Don't forget to set that line to a variable to save it.
To replace the last occurrence of a ma in a string with a period, you can use:
var index = str.lastIndexOf(",");
var newstr = str.substring(0, index) + "." + str.substring(index + 1);
After looking at your code, it seems epascarello's approach is the best. In addition to what he has pointed out, if there are no elements in the array, your string will somewhat abruptly end with: "she likes". To fix this, you could use:
likesString = petObj.name + " is a " + petObj.type + (petObj.likes.length ? ", she likes " + petObj.likes.join(", ") : "") + ".";
For start you can avoid to put the last ma with a code like this:
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
var likesString = petObj.name + " " + " is a " + petObj.type;
if (petObj.likes.length) {
likesString = likesString + " she " + " likes " petObj.likes.join(", ");
}
likesString = likesString + '.'
displayResult();
}
If you still want to use your code you have to test versus lenght-1 as the likes array indexes go from 0 to length-1:
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
var likesString = petObj.name + " " + " is a " + petObj.type + " she " + " likes ";
for (var j = 0; j < petObj.likes.length; j++) {
likesString = likesString + petObj.likes[j] + ((j == petObj.likes.length-1)?'.':', ';
}
displayResult();
}
If you still want to change the string after its construction you can just remove the last two characters
likesString=likesString.replace(/, $/,'.')
That is because replace() returns a new string with the new value instead of changing the string itself, you could do this instead:
likesString = likesString.replace(/,\s$/, ".");
本文标签: arrayshow to remove a comma and replace with a period javascriptStack Overflow
版权声明:本文标题:arrays - how to remove a comma and replace with a period javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744287421a2598947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论