我看到了这个帖子,但我没有看到JavaScript特定的例子.有一个简单string.Empty的JavaScript可用,还是只是检查的情况""?
我有一个包含多个空格的字符串.我想用加号替换它们.我以为我可以用
var str = 'a b c';
var replaced = str.replace(' ', '+');
Run Code Online (Sandbox Code Playgroud)
但它只取代了第一次出现.如何让它替换所有出现的?
我已经阅读了关于javascript trim的这个问题,带有正则表达式的答案.
然后我希望修剪去除Hello和World之间的内部空间.
function myFunction() {
alert("Hello World ".trim());
}
Run Code Online (Sandbox Code Playgroud)
EDITED
为什么我期待!?
废话!显然修剪不会移除内部空间!只有前导和尾随,这就是修剪工作的方式,那么这是一个非常错误的问题,我的道歉.
如何从给定的字符串中删除所有空格.
说:
var str = " abc de fog ";
str.trim();
Run Code Online (Sandbox Code Playgroud)
//给出 - abc de fog AND NOT abcdefog我想要的
如何使用javascript删除所有空白区域?
在我的一个角度应用程序中,当我在ng-repeat中传递键时,我将得到值.
这里,每个排在rowsdata具有类似值'my file1 data', 'my file2 data', 'my file3 data'
但我需要传递它 'myfile1data', 'myfile2data', 'myfile3data'
当我使用rows.replace('','')时,它只删除第一个空格 'myfile1 data', 'myfile2 data', 'myfile3 data'
<tr ng-repeat="data in datas">
<td ng-repeat="rows in rowdatas">{{data[rows.replace(' ','')]}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
编辑
但是当我使用时
<td ng-repeat="rows in rowdatas">{{data[rows.replace(/ /g,'')]}}</td>
Run Code Online (Sandbox Code Playgroud)
我有
Error: a is not a function OPERATORS["/"]@http://loclhost/jcp_standardwork/secure/scripts/vendor/angular/a??ngular.js:5959 OPERATORS["/"]@http://loclhost/jcp_standardwork/secure/scripts/vendor/angular/a??ngular.js:5959 binaryFn/<@http://loclhost/jcp_standardwork/secure/scripts/vendor/angular/angul??ar.js:6292
Run Code Online (Sandbox Code Playgroud)
有人能告诉我一些解决方案吗?
在下面的数组中,如何删除每个字符串中单词之间的空格?我想将“FLAT RATE”转换为“FLATRATE”,将“FREE SHIPPING”转换为“FREESHIPPING”。
我不得不用数组解决问题。我看到了简单字符串案例的解决方案。
我正在尝试删除字符串中的空格,但它似乎不起作用。这不会返回不带空格的字符串:
"{ color: 'blue',".replace(" ",'')和"{ color: 'blue',".trim()
我已经验证这些是空格(字符代码 32),我什至已经这样做了:
var x = "{ color: 'blue',"
x.replace(x[1],'')
仅供参考,在 Chrome 的控制台中执行此操作。我要疯了吗?
我找到了几个关于这个的答案(从文本中删除所有空格 [重复])
但是,没有一个答案在我的情况下有效,如果您有时间,请看一看..
第一步 Mako & Python 模板:为什么我在第一个空格中有新行和空格:
我们使用 Mako 模板和 Python 在我们的视图中生成数据:
<!-- The Python def on the page that pulls in the correct id -->
<%def name="pull_id(contact)">
% if "member" in contact:
${contact["member"]["id"]}
% else:
${contact["id"]}
% endif
</%def>
<%def name="render_contact_row(contact)">
<!-- the def returns the id here -->
<tr data-contact-id='${pull_id(contact)}'>
Run Code Online (Sandbox Code Playgroud)
最初我直接在<tr>标签中使用了 Python 代码,但是它生成了可见的换行符。现在使用<%def至少它将所有内容保留在 1 行,但 HTML 中仍有一些额外的空格
现在我的jQuery:
$('.btn_hide').live("click", function(event) {
// gets the id number from the data tag …Run Code Online (Sandbox Code Playgroud) 我正在尝试使此回文生成器正常工作,但我无法弄清楚如何使js从多字回文中的单词之间删除空白。赛车不断回来虚假。racecar恢复为真,因此部分代码正在运行。如何让JS忽略“种族”和“汽车”之间的空白,使赛车恢复原状?
function palindrome(word) {
var len = word.length;
word = word.replace(/ +/g, "");
for (var i = 0; i < Math.floor(len/2); i++ ) {
if (word[i] !== word[len - 1 - i]) {
return "FALSE";
}
}
return "TRUE";
}
console.log(palindrome("race car"))
Run Code Online (Sandbox Code Playgroud) javascript ×9
jquery ×2
string ×2
trim ×2
angularjs ×1
html ×1
is-empty ×1
null ×1
python ×1
whitespace ×1