Remove last comma (and possible whitespaces after the last comma) from the end of a string
This will remove the last comma and any whitespace after it:
str = str.replace(/,\s*$/, "");
It uses a regular expression:
The
/
mark the beginning and end of the regular expressionThe
,
matches the commaThe
\s
means whitespace characters (space, tab, etc) and the*
means 0 or moreThe
$
at the end signifies the end of the string
how to replace last comma with and
var test = "Service Control Manager repeated 5 times, " +
"Microsoft-Windows-DistributedCOM repeated 2 times, " +
"Control Manager repeated 6 times.";
var lastComma = test.LastIndexOf(',');
if (lastComma != -1) test = test.Remove(lastComma, 1).Insert(lastComma, " and");
Replace final comma in a string with and
You probably want
,(?=[^,]+$)
for example:
"1, 2, 3, 4".replace(/,(?=[^,]+$)/, ', and');
(?=[^,]+$)
checks that there are no more commas after this comma. (?!.*,)
would also work.
You can even check there isn't already an and
:
,(?!\s+and\b)(?=[^,]+$)
Working example: https://regex101.com/r/aE2fY7/2
Replace the last comma with an & sign
Pop off the last element, implode the rest together then stick the last one back on.
$bedroom_array = array('studio', 'one_bed', 'two_bed', 'three_bed', 'four_bed');
$last = array_pop($bedroom_array);
$string = count($bedroom_array) ? implode(", ", $bedroom_array) . " & " . $last : $last;
Convert &
to the entity &
if necessary.
How to replace last comma in string with and using php?
To replace only the last occurrence, I think the better way is:
$likes = 'Apple, Samsung, Microsoft';
$likes = substr_replace($likes, ' and', strrpos($likes, ','), 1);
strrpos finds the position of last comma, and substr_replace puts the desired string in that place replacing '1' characters in this case.
Replace last comma in character with &
One option is stri_replace_last
from stringi
library(stringi)
stri_replace_last(x, fixed = ',', ' &')
#[1] "char1, char2 & char3"
Replace last instance of comma with 'and' in a string
I think the replaceFirst
is better than replaceAll
for you because you want to replace only once not all, and it run faster than replaceAll
.
using
${number}
to capturing groups."a, b, c".replaceFirst(",([^,]+)$", " and$1"); // return "a, b and c"
using positive lookahead:
"a, b, c".replaceFirst(",(?=[^,]+$)", " and"); // return "a, b and c"
Related Topics
Using a Variable as an Operator
Key of Null Variable Equals Null Not Error
Patterns for PHP Multi Processes
Laravel Expected Response Code 250 But Got Code "530"
Class App\Http\Controllers\Usercontroller Does Not Exist
Using a Variable as an Operator
Nohup on Windows, Exec Without Waiting for Finish
Differencebetween .= and += in PHP
Ip Address Is Showing in Form Action with Codeigniter Http://::1/Codeigniter/ in HTML Sourcecode
How to Create a Nested Array Out of an Array in PHP
Using PHP Replace Regex with Regex
PHP Upload Image to Remote Server with Curl
Symfony 2 Forms Entity Field Type Grouping
Dump Facility in C++ Like Var_Dump() in PHP
Laravel 5 Multiple Download File
Set Value of Single Object in Multidimensional Array in Twig Template