iOS Phonegap regex replace

2019-08-10 10:46发布

I might just be doing something obvious and dumb here, but I can get this code to work in the Chrome console, but when I run it in my phonegap app it just doesn't do anything.

I'm pulling in some messages from an website API to a phonegap companion app. The problem is the messages have links in them to the website. I want to use regex to change the href to an onClick so it works with my app.

If I run this code in Chrome it's fine:

avar = '<a href="view.php?id=63">Click here to view details</a>';
bvar = avar.replace(
    /<a href="view\.php\?id=([0-9])*">/gi,
    '<a onClick="navigator.notification.alert(\'ok\')">AAA'
);

I stripped the regex right back to this, and it still fails

bvar = avar.replace(
    /<a href="/gi,
    'TEST'
);

Again that works in Chrome and not the Phonegap app. Any ideas?

1条回答
Summer. ? 凉城
2楼-- · 2019-08-10 11:35

if it seems to have issues with the ", try putting brackets around what you are regex-ing:

bvar = avar.replace(
    /(<a href=")/gi,
    'TEST'
);

also, using the console.log, just check that the regex is getting registered in js correctly.

var rgxtest = /(<a href=")/gi
console.log(rgxtest);
查看更多
登录 后发表回答