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 to message. 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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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