admin管理员组文章数量:1414628
I am trying to check whether string contain specific given sentence in string or not. I have below string in that I want to check 'already registered'
sentence present or not however string is contain array so I couldn't get.
message = 'User error fabric-ca request register failed with errors [[{"code":0,"message":"Registration of \'yandgsub15\' failed: Identity \'yandgsub15\' is already registered"}]]' }
I tried below function, however I couldn't get it though it. please anybody have solution?
var n = error.includes("registered");
I am trying to check whether string contain specific given sentence in string or not. I have below string in that I want to check 'already registered'
sentence present or not however string is contain array so I couldn't get.
message = 'User error fabric-ca request register failed with errors [[{"code":0,"message":"Registration of \'yandgsub15\' failed: Identity \'yandgsub15\' is already registered"}]]' }
I tried below function, however I couldn't get it though it. please anybody have solution?
var n = error.includes("registered");
Share
Improve this question
asked Oct 20, 2020 at 7:15
Abhijeet1234Abhijeet1234
451 silver badge6 bronze badges
5
- 2 message.indexOf("already registered") >= 0 – maraca Commented Oct 20, 2020 at 7:19
-
You used the incorrect variable to get your expected result. Try to change
error
tomessage
.var n = message.includes("registered");
– Jonnel VeXuZ Dorotan Commented Oct 20, 2020 at 7:21 - your string name is 'message' but you're searching your word in a string named error, your first real problem is here – Axel Moriceau Commented Oct 20, 2020 at 7:22
- right I forget to do it like const message = error.message – Abhijeet1234 Commented Oct 20, 2020 at 7:26
- var n = message.includes("registered"); – Banny Commented Oct 20, 2020 at 7:28
2 Answers
Reset to default 3You are trying to search for "registered" in a string called error but there isn't try this:
message = 'User error fabric-ca request register failed with errors [[{"code":0,"message":"Registration of \'yandgsub15\' failed: Identity \'yandgsub15\' is already registered"}]]' }
var n = message.includes("registered");
I think you just don't search on the good variable
In your example you're trying to search "registered"
on error
and not on message
The code below is working
message = 'User error fabric-ca request register failed with errors [[{"code":0,"message":"Registration of \'yandgsub15\' failed: Identity \'yandgsub15\' is already registered"}]]'
var n = message.includes('registered')
console.log(n) // true
本文标签: How to check if a string contains specific words in javascriptStack Overflow
版权声明:本文标题:How to check if a string contains specific words in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745177694a2646320.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论