Mar*_*ark 1 html javascript jquery
我有这样的字符串:
"Car is blue"
Run Code Online (Sandbox Code Playgroud)
字符串也可以是这样的:
"Flower is not at all beautiful"
Run Code Online (Sandbox Code Playgroud)
我有这样的HTML:
<input class="subject" />
<select class="isornot">
<option>is</option>
<option>is not</option>
</select>
<input class="adjective" />
Run Code Online (Sandbox Code Playgroud)
我需要拆分字符串并将其放入表单中的适当位置.对于第一个字符串,主题val应该是car,应该选择,并且形容词值应该是蓝色.对于第二个字符串,花是主题,不是选择选项,'at all beautiful'应该是形容词.
因此,拆分应该是或不是.不确定如何去做.谢谢.
干得好:
var str = "Car is not blue";
var match = str.match(/^(.+?)\s+(is(?:\snot)?)\s+(.+)$/);
if (match) {
$('input.subject').val(match[1]);
$('select.isornot').val(match[2]);
$('input.adjective').val(match[3]);
} else {
alert("Could not parse message.");
}
Run Code Online (Sandbox Code Playgroud)
参考文献: