I'm trying to program a kind of chatterbot in JavaScript and am having trouble substituting a * character using JavaScript's regular expressions.
For example, code like this:
The idea is that the "raw trigger" uses simple *'s to represent wildcards on the user's message, but to actually match the message on the backend the * must become a (.+?).
If I manually type in (.+?) in place of the * there, it works. Just for some reason the replace function won't replace the * with (.+?)
And no, I can't just change things to use (.+?) everywhere instead of *'s. What I'm programming is a RiveScript interpreter in JavaScript (rivescript.com), and RiveScript is intended to be a simplistic chatterbot reply language and looks like this:
And thus, the "raw" reply data is going to contain asterisks in this way. In the RiveScript code the triggers aren't supposed to be thought of as regular expressions, but as just simple triggers that are easy to read like that.
I've tried the following variants:
The final regexp must have the ^ and $ characters in them. If I remove them from the code it will strangely match, but I'm pretty sure it isn't matching properly (i.e. the * in there is probably matching like "\s*" because of the space in front of it, and not like "\s(.+?)" like it should). The punchline is that the literal * symbol just isn't being replaced with a (.+?) to begin with.
Soo how do you match a literal asterisk in a JavaScript regular expression?
-------------
Cuvou.com | My personal homepage
For example, code like this:
Code:
<html>
<head>
<script>
var debug = new Array();
var d = 0;
var trigger = 'my name is *';
var reply = 'Nice to meet you.';
window.alert("Raw trigger: " + trigger);
var message = "my name is soandso";
// convert the trigger for matching
//trigger.replace(/\*/, "(.+?)");
trigger.replace(/\x2a/, "(.+?)");
window.alert("New trigger: " + trigger);
var re = new RegExp("^" + trigger + "$");
// match
if (message.match(re)) {
window.alert(reply);
}
else {
window.alert("No reply matched");
}
</script>
</head>
<body>
</body>
</html>
The idea is that the "raw trigger" uses simple *'s to represent wildcards on the user's message, but to actually match the message on the backend the * must become a (.+?).
If I manually type in (.+?) in place of the * there, it works. Just for some reason the replace function won't replace the * with (.+?)
And no, I can't just change things to use (.+?) everywhere instead of *'s. What I'm programming is a RiveScript interpreter in JavaScript (rivescript.com), and RiveScript is intended to be a simplistic chatterbot reply language and looks like this:
Code:
+ hello bot
- Hello, human.
+ i am * years old
- <set age=<star>>I will remember you are <star> years old.
And thus, the "raw" reply data is going to contain asterisks in this way. In the RiveScript code the triggers aren't supposed to be thought of as regular expressions, but as just simple triggers that are easy to read like that.
I've tried the following variants:
Code:
str.replace(/\*/, "(.+?)"); // does nothing
str.replace(/*/, "(.+?)"); // syntax error
str.replace(/\\*/, "(.+?)"); // does nothing
str.replace(/\x2a/, "(.+?)"); // does nothing
str.replace(/\o052/, "(.+?)"); // does nothing
The final regexp must have the ^ and $ characters in them. If I remove them from the code it will strangely match, but I'm pretty sure it isn't matching properly (i.e. the * in there is probably matching like "\s*" because of the space in front of it, and not like "\s(.+?)" like it should). The punchline is that the literal * symbol just isn't being replaced with a (.+?) to begin with.
Soo how do you match a literal asterisk in a JavaScript regular expression?
-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'