我想得到的结果是:E1,E5。这可能在jQuery甚至是PHP中吗?
更新:
我想将它应用于我的json_encoded数据。
var array = $('#lot_numbers').val(details.lot_number);
的数据为 E1,1,2,3,4,5,E5
$('#end_unit').change(function() {
if($(this).val().length == 0) {
$('#display_lot_numbers').hide('slow');
} else {
if($(this).val() == 'Yes') {
var allotment_code = $('#allotment_code').val();
$.post(base_url('ajax/ajaxGetUnits.php'), {
allotment_code:allotment_code
},
function(data, status) {
var details = JSON.parse(data);
$('#display_lot_numbers').show('slow');
var array = $('#lot_numbers').val(details.lot_number);
var nonnumbers = $.grep(array.split(","), function(n, i) {
return isNaN(n);
}).join(",");
console.log(nonnumbers);
});
}
}
})
答案 0 :(得分:3)
试试这个:
首先拆分字符串并创建一个数组,然后使用grep
方法通过isNaN
函数获取非数字。
var lot_numbers = 'E1,1,2,3,4,5,E5';
var nonnumbers = $.grep(lot_numbers.split(","), function(n, i) {
return isNaN(n);
}).join(",");
console.log(nonnumbers);
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
答案 1 :(得分:0)
甚至没有使用jQuery - 只是常规的js:
var lot_numbers = 'E1,1,2,3,4,5,E5';
var arr = lot_numbers.split(",");
var result = arr.filter(function(i) {
return i.match(/[a-z]/i);
});
return result.join();
答案 2 :(得分:0)
尝试这个
var lot_numbers = 'E1,1,2,3,4,5,E5';
var arr = lot_numbers.split(',');
var patt = /[a-zA-Z]\d/;
for (var i = 0; i < arr.length; i++) {
if (patt.test(arr[i])) {
alert(arr[i]);
}
}
答案 3 :(得分:0)
Check below solution.
var lot_numbers = 'E1,1,2,3,4,5,E5';
var array=lot_numbers.split(",");
for(var i = 0; i < array.length; i++) {
// Check number or not
if(isNaN(array[i]))
alert(array[i]);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 4 :(得分:0)
不需要jQuery,普通的javascript会有所帮助。
var lot_numbers = 'E1,1,2,3,4,5,E5';
var arr = lot_numbers.split(",");
var alphanum = arr.filter(function(x) {
return isNaN(x);
}).join();
console.log(alphanum); // "E1,E5"