admin管理员组文章数量:1318563
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.
本文标签: javascriptUsing replace() to change period to underscoreStack Overflow
版权声明:本文标题:javascript - Using .replace() to change period to underscore - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742045044a2417734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论