最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using .replace() to change period to underscore - Stack Overflow

programmeradmin1浏览0评论

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
 |  Show 4 more ments

3 Answers 3

Reset to default 4

You 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.

发布评论

评论列表(0)

  1. 暂无评论