Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regexp with an Asterisk 1

Status
Not open for further replies.

Kirsle

Programmer
Jan 21, 2006
1,179
US
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:

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;'
 
You just have to capture the return. (\x2a or \* would have the same effect.)
>trigger.replace(/\x2a/, "(.+?)");
[tt]trigger=trigger.replace(/\x2a/, "(.+?)");[/tt]

Furthermore some details.

In the cases where more the one wildcard is admissible, you have to assert global flag.
[tt]trigger=trigger.replace(/\x2a/[blue]g[/blue], "(.+?)");[/tt]

Also in the above case, the wildcard (*) in the gui actually exclude empty input; if you allow empty input, you may as well just use again (*) in the replacing part. [tt]trigger=trigger.replace(/\x2a/, "(.[blue]*[/blue]?)");[/tt]
 
ahhh. thanks! it's always something simple like that. :)

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top