I'm trying to replace a period in a string to an underscore using the javascript .replace()
function. For example, if the string was Project.Build
, I would want it to bee Project_Build
. Unfortunately, the method isn't currently doing that. The period continues to remain a period.
This line of code is trimming off all white space from the string. It is also replacing all spaces with underscores as well. This part is currently working.
var rowID = $.trim(new String(status.teamCityProject).toLowerCase().replace(/ /g, "_").replace(/\./g, '_')) + status.id;
$("tr#" + rowID + " td.status").html(status.status).css({ 'color': statusColor, 'font-weight': 'bolder' })
I'm trying to replace a period in a string to an underscore using the javascript .replace()
function. For example, if the string was Project.Build
, I would want it to bee Project_Build
. Unfortunately, the method isn't currently doing that. The period continues to remain a period.
This line of code is trimming off all white space from the string. It is also replacing all spaces with underscores as well. This part is currently working.
var rowID = $.trim(new String(status.teamCityProject).toLowerCase().replace(/ /g, "_").replace(/\./g, '_')) + status.id;
$("tr#" + rowID + " td.status").html(status.status).css({ 'color': statusColor, 'font-weight': 'bolder' })
Share
Improve this question
edited May 24, 2012 at 19:57
Lisa Lemons
asked May 24, 2012 at 19:47
Lisa LemonsLisa Lemons
1604 silver badges13 bronze badges
9
- 3 Regex seems ok, and it works fine in a fiddle: jsfiddle/BDZWK – Rory McCrossan Commented May 24, 2012 at 19:49
-
Looks correct to me as well. Maybe
status.id
has a period in it? – jimbo Commented May 24, 2012 at 19:50 - 5 Are you assigning it back to the orginal string... .replace will not change the original text. – Selvakumar Arumugam Commented May 24, 2012 at 19:51
- 1 @Vega you should post your ment as the answer :) – fardjad Commented May 24, 2012 at 19:52
- 1 @Vega Winner winner, chicken dinner. I'd make that an answer as it seems most likely. – Rory McCrossan Commented May 24, 2012 at 19:52
3 Answers
Reset to default 4You should assign or update the modified string to the var and use it.. .replace
will not update the original text. (Also removed new String)
var rowID = $.trim(status.teamCityProject.toLowerCase().replace(/ /g, "_").replace(/\./g, '_')) + status.id;
$("tr#" + rowID + " td.status").html(status.status).css({ 'color': statusColor, 'font-weight': 'bolder' })
DEMO
JavaScript strings are immutable. .replace()
returns a new string you have to do something with.
var newStatus = $.trim(status.teamCityProject
.toLowerCase()
.replace(/ /g, "_")
.replace(/\./g, '_')) + status.id;
// do something with newStatus
What you have works.
However, you can condense this into a single replace()
and avoid String
with the following. Also note that your original post was missing the closing )
for trim()
.
$.trim((status.teamCityProject).toLowerCase().replace(/[ .]/g, "_") + status.id);
See it in action.