简体   繁体   English

使用正则表达式,我怎样才能在句子中得到“músicas”或“áaudio”这个词?

[英]Using Regex, how can I get the words "músicas" or "áudio" in a sentence?

I'd like to check a sentence in JavaScript.我想在 JavaScript 中检查一个句子。 I'm using Web Speech API in Portuguese (PT-BR) by Google.我正在使用 Google 提供的葡萄牙语网络语音 API (PT-BR)。 So code follows:所以代码如下:

[...]

// Writes the spoken sentence on a field
document.getElementById('text').innerHTML = result;

// Should check whether "músicas" is in sentence or not
if (/músicas/g.test(result) == true)
{
  document.getElementById("output").innerHTML = "Inside sentence";
}
else
{
  document.getElementById("output").innerHTML = "Not inside sentence";
}

[...]

The variable result stores the spoken sentence in Portuguese, for example, Eu gosto de músicas (Which means I like musics ).变量result存储葡萄牙语的口语句子,例如, Eu gosto de músicas (这意味着我喜欢音乐)。 Then, the conditions should check, but it's not working with accentuations.然后,应该检查条件,但它不适用于重音。

By the way, if I check something without accentuation, such as /fotos/g , it works perfectly!顺便说一句,如果我检查一些没有重音的东西,例如/fotos/g ,它完美地工作!

Can anybody help me with this probably simple Regex issue?有人可以帮我解决这个可能很简单的正则表达式问题吗?

Thank you.谢谢你。

To match accented vowels in a JavaScript regex, we can use the unicode character class:要匹配 JavaScript 正则表达式中的重音元音,我们可以使用 unicode 字符类:

if (/m[\u00FA]sicas/g.test(result) == true) {
    document.getElementById("output").innerHTML = "Inside sentence";
}

You can refer here for a table of accented characters and their Unicode equivalents.您可以在此处参考重音字符表及其 Unicode 等效项。

Note that as others have mentioned, in pure JavaScript you do not need Unicode classes for this, but apparently you have an application which requires this.请注意,正如其他人所提到的,在纯 JavaScript 中,您不需要为此使用 Unicode 类,但显然您有一个需要此功能的应用程序。

Demo演示

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

相关问题 如何使用Regex从句子中删除特定的字符串? - How can I remove specific strings from a sentence using Regex? 如何使用 javascript 获取句子中的大写单词 - How to get uppercase words in sentence using javascript 使用正则表达式提取句子中的最后几个单词? - Extract the last few words in a sentence using regex? 如何排除句子中的某些单词。 正则表达式? - How to exclude certain words in sentence. regex? 使用正则表达式如何获取URL中两个单词之间的匹配? - Using Regex how can i get a match between two words in a url? 如何用正则表达式使很多单词在句子中加粗? - How to make many words bold in a sentence with regex? 如何使用正则表达式匹配包含特定单词的每个句子? - How can I match every sentence that contains a specific word using regex? 使用jquery / JS / CSS如何将单词附加到句子中,以便逐句推送句子 - Using jquery/JS/CSS how do I append words to a sentence so the sentence is pushed up word by word 我使用的是纯 JavaScript,但我继续收到以“不是函数”结尾的错误。 如何做到这一点,以便我可以检测单词并做出相应的回复? - I'm using pure JavaScript but I continue to get errors that end with "is not a function". How do make it so I can detect words and reply accordingly? 正则表达式匹配句子中的部分单词 - Regex match partial words in a sentence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM