在问号前找到单行文本的正则表达式是什么?

时间:2014-10-17 21:23:05

标签: javascript regex

在javascript中,我只想在换行符之前匹配所有文本实例,例如:

It would not match this text.

How would I match this text?

我正在尝试自动突出显示textarea的所有问题。

1 个答案:

答案 0 :(得分:3)

非常简单:

^.*\?$

Demo


^$锚定到字符串的开头和结尾。 .*匹配0+个字符(换行符除外)。 \?匹配字符串结尾前的问号。在Javascript中,使用.match()



var regex = /^.*\?$/gm;

console.log('It would not match this text.'.match(regex));
console.log('How would I match this text?'.match(regex));

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
&#13;
&#13;
&#13;

相关问题