我正在尝试在textbox / textarea值中查找字符串的实例,到目前为止,我已附加了我的代码:
$('.message-contents').on('keyup',function(){
textbox_value = $(this).val();
//working on code
var regex = /#{\w.*}/;
//working on http://regex101.com
//var regex = /^#{\w.*}$/;
window.variable_found = 0
if(textbox_value.match(regex)!= null){
console.log('found');
variable_length = ($(this).val().trim().length);
console.log(variable_length);
window.variable_found = variable_length ;
}
else{
console.log('not found');
}
});

<textarea class="message-contents"></textarea>
&#13;
但这也适用于字符串的后续输入。如何修改我的代码以便在下面给出我想要的结果:
#{string}asasasasas = 9 //desired output;
#{string}asasasasas = 19//current output
我想从&#39;#{&#39;开始获取字符串的长度。结束于&#39;}&#39;。基于所需的输出,即9。
答案 0 :(得分:0)
您无法正确访问匹配结果。试试吧:
Integer[] indexes = new Integer[]{0,2,8,5};
String[] sources = new String[]{"how", "are", "today", "you"};
String[] targets = new String[]{"I", "am", "thanks", "fine"};
Integer[] sortedArrya = Arrays.copyOf(indexes, indexes.length);
Arrays.sort(sortedArrya);
String[] sortedSourses = new String[sources.length];
String[] sortedTargets = new String[targets.length];
for (int i = 0; i < sortedArrya.length; i++) {
int intValus = sortedArrya[i];
int inx = Arrays.asList(indexes).indexOf(intValus);
sortedSourses[i] = sources[+inx];
sortedTargets[i] = targets[+inx];
}
System.out.println(sortedArrya);
System.out.println(sortedSourses);
System.out.println(sortedTargets);
$('.message-contents').on('keyup',function(){
textbox_value = $(this).val();
var regex = /#{\w.*}/;
// The result can be accessed through the `m`-variable.
if((m = regex.exec(textbox_value)) !== null){
console.log('found');
variable_length = m[0].length;
console.log(variable_length);
}
else{
console.log('not found');
}
});