tux-world
tux-world

Reputation: 2720

nodejs equivalent Pattern.compile

this code on java/android work fine to find all texts between <tag> and </tag>

whats equivalent of my code on java on nodejs/javascript?

private static final Pattern TAG_REGEX = Pattern.compile("<tag>(.+?)</tag>");

private void getTagValues(final String str) {
    final Matcher matcher = TAG_REGEX.matcher(str);
    while (matcher.find()) {
        // print found text
    }
}

for example:

var value = "<tag>text 1</tag> aaaaa <tag>text 2</tag>";
var regex = '<tag>(.+?)</tag>';
var match = regex.test(value);
while (matcher.find()) {
    if (match) {
        console.log(value + 'is not a number that has between 1,20 digits');
    }
}

Upvotes: 0

Views: 407

Answers (1)

Syed Ayesha Bebe
Syed Ayesha Bebe

Reputation: 1448

I changed your code try this.This code is working as your expectations.Hope this helps for you.

var express=require('express');
var app=express();
app.get('/tag',function(request,response)
{
var value="<tag>text 1</tag>,aaaaa<tag>text 2</tag>.";
var regex="<tag>(.+?)</tag>.";
var result=value.match(/<tag>(.*?)<\/tag>/g).map(function(val){
var first=val.replace(/<\/?tag>/g,'');
console.log(first);
});
var match=regex.match(/<tag>(.*?)<\/tag>/g).map(function(valy){
var second=valy.replace(/<\/?tag>/g,'');
console.log(second);
});
response.send("data send");
});
app.listen(3000,function(){
console.log("Server is listening on http://localhost:3000");
});

See output in a browser as localhost:3000/tag and see the text between tags in terminal.

Upvotes: 1

Related Questions