简体   繁体   English

如何在文本区域的两个特殊符号(%)之间获取文本?

[英]How to get the text between two special symbols(%) in a textarea?

How can i get the text between two % symbols in a textarea ??? 如何在文本区域的两个%符号之间获取文本? I tried using jquery .keypress() event and got the result upto a certain level. 我尝试使用jquery .keypress()事件,并将结果提高到一定水平。 But the keypress event will not get triggered if i delete or press backspace. 但是,如果我删除或按Backspace键,则不会触发keypress事件。 If i go with .keyup() or .keydown(), how to detect the "%" character ... Here is what i tried out 如果我使用.keyup()或.keydown(),如何检测“%”字符...这是我尝试过的

var flag = false,
string = '';
$("#target").keypress(function(event) {
//  console.log(event,String.fromCharCode(event.keyCode));
if ( event.which == 37 ) {  
 if(flag === true){
  flag = false;      
  alert("REsult: "+ string.slice(1));
 }      
 flag= true;
}

//  console.log(flag, event.which);
if(flag === true){
 string = string + String.fromCharCode(event.which);
}
 console.log(string);   
});

Here is the bin 这是垃圾桶

Am i going in the right path or is there any other better way... Please help me with your suggestions 我走的路是正确的还是还有其他更好的方法...请帮助我提供您的建议

try this, 尝试这个,

var test_str = "text to get % Other text.... migh have % s ...";
var start_pos = test_str.indexOf('%') + 1;
var end_pos = test_str.indexOf('%',start_pos);
var text_to_get = test_str.substring(start_pos,end_pos)
alert(text_to_get);

How can i get the text between two % symbols in a textarea ??? 如何在文本区域的两个%符号之间获取文本?

var matches = /%([^%]*)%/.exec($("#target").val());
var result = matches ? matches[1] : null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM