我有一个句子“欢迎来到项目中,您是a”,在这里我需要删除“ a”,该句子仅在句子包含最后一个字母为“ a”时才删除,否则我已经尝试过此代码,但在这里不起作用
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="messages"></ul>
var text = 'Welcome to project, are you a (1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident)';
var regex = /\((.*)\)/;
var m;
m = regex.exec(text);
console.log(m[1]);
// 1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident
var list = m[1];
var items = list.split(/([\d]. )/g);
var message = $('.messages');
var introText = text.split('(');
var test = introText[0]
alert(test.replace(/\a$/, ""));
答案 0 :(得分:0)
在下面使用以下脚本:
var text = 'Welcome to project, are you a (1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident)';
var regex = /\((.*)\)/;
var m;
m = regex.exec(text);
console.log(m[1]);
// 1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident
var list = m[1];
var items = list.split(/([\d]. )/g);
var message = $('.messages');
var introText = text.split('(');
var test = introText[0].trim();
//alert(test.replace(/\a$/, ""));
if(test.slice(-2) == " a"){
alert(test.slice(0,-1));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="messages"></ul>