我有一个代表数字的js字符串。我想从字符串的开头仅删除不必要的0,如果不存在数字,则用正则表达式在逗号前放置0:
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
@if(Session::has('message'))
<div class="alert">
<div class="alert alert-success">
<strong>{{ Session::get('message') }}</strong>
</div>
</div>
@endif
@if ($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
@endif
{!! Form::open([
'route' => 'submitTestForm'
]) !!}
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
<li class="nav-item @if($errors->has('name') === true && $errors->has('email') === false || $errors->has('name') === true && $errors->has('email') === true) active @endif">
<a class="nav-link @if($errors->has('name') === true && $errors->has('email') === false || $errors->has('name') === true && $errors->has('email') === true || $errors->has('name') === false && $errors->has('email') === false) active @endif" id="pills-formOne-tab" data-toggle="pill" href="#pills-formOne" role="tab" aria-controls="pills-home" aria-selected="true">Form One</a>
</li>
<li class="nav-item @if($errors->has('name') === false && $errors->has('email') === true) active @endif">
<a class="nav-link @if($errors->has('name') === false && $errors->has('email') === true) active @else disabled @endif" id="pills-formTwo-tab" data-toggle="pill" href="#pills-formTwo" role="tab" aria-controls="pills-profile" aria-selected="false">Form Two</a>
</li>
</ul>
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade @if($errors->has('name') === true && $errors->has('email') === false || $errors->has('name') === true && $errors->has('email') === true) show active in @elseif($errors->has('name') === false && $errors->has('email') === false) show in active @endif" id="pills-formOne" role="tabpanel" aria-labelledby="pills-formOne-tab">
<div class="form-group">
<input type="text" name="name" value="{{ old('name') }}" placeholder="Enter Name">
</div>
</div>
<div class="tab-pane fade @if($errors->has('name') === false && $errors->has('email') === true) show active in @endif" id="pills-formTwo" role="tabpanel" aria-labelledby="pills-formTwo-tab">
<div class="form-group">
<input type="email" name="email" value="{{ old('email') }}" placeholder="Enter Email">
</div>
</div>
</div>
<hr>
<div class="form-control">
<button class="btn btn-sm btn-success">Submit</button>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
我尝试过:
var regex = ?
var replace = ?
new = "0001,3".replace(regex, replace); // 1,3
new = ",3".replace(regex, replace); // 0,3
new = "000,34".replace(regex, replace); // 0,34
new = "0201,3".replace(regex, replace); // 201,3
new = "0002,03".replace(regex, replace); // 2,03
但是看起来第二次替换不起作用。
答案 0 :(得分:2)
0
。
function trim(txt) {
return txt.replace(/^(0*)(,?)/, (full, zeros, end) => end ? "0," : "");
// If the zeros are directly followed by a `,` then keep a `0`
// use `function (){return arguments[2] ? "0," : "";}` for ES5
}
console.log(trim("0001,3"));
console.log(trim(",3"));
console.log(trim("000,34"));
console.log(trim("0201,3"));
console.log(trim("0002,03"));
0
)
function trim(txt) {
return String(+(txt.replace(",", "."))).replace(".", ",");
}
console.log(trim("0001,3"));
console.log(trim(",3"));
console.log(trim("000,34"));
console.log(trim("0201,3"));
console.log(trim("0002,03"));
答案 1 :(得分:0)
function trimZeroesLeft(txt) {
return ("0" + txt).replace(/^0+(?=\d)/g, "");
}
function trimZeroesRight(txt) {
return txt.replace(/,0*$|(,\d+?)0+$/g, "$1");
}
function trimZeroes(txt) {
return ("0"+txt).replace(/^0+(?=\d)|,0*$|(,\d+?)0+$/g, "$1");
}
let arr = ["0001,3", ",300", "000,34", "0201,30", "0002,03", "012,0", "0,00"];
console.log("trimZeroesLeft", arr.map(v => `${v} -> ${trimZeroesLeft(v)}`));
console.log("trimZeroesRight", arr.map(v => `${v} -> ${trimZeroesRight(v)}`));
console.log("trimZeroes", arr.map(v => `${v} -> ${trimZeroes(v)}`));
.as-console-wrapper{top:0;max-height:100%!important}