Javascript regex from JSON -
i tried searching this, results coming php had simple json_decode create array etc.
sadly seemed (unless didn't see it) javascript didn't have function.
anywhom, problem have relatively little json document looks this:
{ "greetings" : [ "hey", "sup", "hello", "hola", "yo", "howdy", "hi", "hello bot" ] } (there's more in entire json, need)
i to, using javascript, check if of these nowadays in message shown below:
function isgreeting(message) { homecoming dict.greetings.indexof(message) != -1; } var re = '/\b' + dict.greetings.indexof(message) + '\b/i'; if (isgreeting(message)) { console.log(message.search(re)); } currently somehow works, detects greetings if not in capitals, them detected no matter how it's written, shown effort regex.
how go about, no matter how greet (as long as, of course, on list) kind of capitals, log it? "hey" still log, if json says "hey" etc.
how go about, no matter how greet (as long as, of course, on list) kind of capitals, log it?
try javascript string tolowercase() method or javascript string touppercase() method.
convert message in either case based on dict.greetings case check using indexof() method within isgreeting method.
since it's json string, can seek jquery.parsejson() takes well-formed json string , returns resulting javascript object.
for e.g:
var obj = jquery.parsejson( '{ "name": "john" }' ); alert( obj.name === "john" ); javascript regex json
No comments:
Post a Comment