次のテキストの部分文字列(REGEX)を検索するJS関数を作成しようとしています。私はあなたがむしろ質問したいのオプションの色を変更しようとしています。
文字列の例
Would you rather be on a survival reality show or dating game show?
ソリューションの例
Would you rather <span class="highlight1"> be on a survival reality show </span> or <span class="highlight2"> dating game show? </span>
以下のコードを試しましたが、正規表現がわかりません。
HTML:
<div class="question">
<p>Would you rather be on a survival reality show or dating game show?</p>
</div>
CSS:
.red{color:red;}
JS:
$(document).ready(function(){
$('.question').html(function(_, html){
return html.replace(**WHAT DO I PUT HERE**, '<span class="red">$1</span>');
});});
REGEX-上記のコードでは機能しません。
(?<=Would you rather)(.*)(?=or) 1st Part
(?<=or)(.*)(?=) 2nd Part
これらの正規表現をJSで機能させるには、いくつかの助けが必要です。彼らは何もしていないようです。私はそのような初心者です。
質問の最初の部分だけを取得するための正規表現は、
/^Would you rather(.*)(:? or .*)$/
^^ find 'or' and the rest of question
https://jsfiddle.net/9pwbj58g/1/
私はあなたがそれで何をしたいのか、わかりません、代わりのalert
いずれかとすることができる$(this).html(...)
か何か。
両方の部分を変更するには、例えば。
/^(Would you rather)(.*)(:? or )([^?]+)\?$/
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加