RegEx解析复杂的xml-javascript

RegEx解析复杂的xml-javascript,javascript,xml,regex,parsing,Javascript,Xml,Regex,Parsing,我在受限的Javascript环境中工作,没有xml解析器或dom访问权限 格式如下: <gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile">206 555 1212</gd:phoneNumber> 206 555 1212 我需要获取字符串[]值:mobile,206 555 1212 每次值都不同,但标记始终相同 然后我需要能够替换这些值,例如:home,555-555-5555 这可以在正则

我在受限的Javascript环境中工作,没有xml解析器或dom访问权限

格式如下:

<gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile">206 555 1212</gd:phoneNumber>
206 555 1212
我需要获取字符串[]值:mobile,206 555 1212

每次值都不同,但标记始终相同

然后我需要能够替换这些值,例如:home,555-555-5555

这可以在正则表达式中完成吗

看一看 看一看

到目前为止,这就是我所拥有的,它是有效的,但是有更好的方法吗

"<gd:phoneNumber rel=http://schemas.google.com/g/2005#mobile>206 555 1212</gd:phoneNumber>".replace(/#.*</g, '#home>111-111-1111<')

“206 555 1212”。替换(/#。*这是我到目前为止所做的,它可以工作,但是有更好的方法吗

"<gd:phoneNumber rel=http://schemas.google.com/g/2005#mobile>206 555 1212</gd:phoneNumber>".replace(/#.*</g, '#home>111-111-1111<')

“206 555 1212”.replace(/#。*此操作检索匹配项并执行替换:

var testString = '<gd:phoneNumber rel=http://schemas.google.com/g/2005#mobile>206 555 1212</gd:phoneNumber>';

var regex = /.*#(\w+)">(.*)</i;

// matches[1] will be "mobile" and matches[2] will be "206 555 1212"
var matches = regex.exec(testString);

// Replace #mobile with #home
testString = testString.replace(matches[1], 'home');

// Replace the phone number with 555 555 5555
testString = testString.replace(matches[2], '555 555 5555');
var testString='206 555 1212';

var regex=/.*(\w+)>(.*)这将检索匹配项并执行替换:

var testString = '<gd:phoneNumber rel=http://schemas.google.com/g/2005#mobile>206 555 1212</gd:phoneNumber>';

var regex = /.*#(\w+)">(.*)</i;

// matches[1] will be "mobile" and matches[2] will be "206 555 1212"
var matches = regex.exec(testString);

// Replace #mobile with #home
testString = testString.replace(matches[1], 'home');

// Replace the phone number with 555 555 5555
testString = testString.replace(matches[2], '555 555 5555');
var testString='206 555 1212';
var regex=/.*#(\w+)>(.*)有一个仅基于regex的变量。您可以将其包含在项目中

//var xml2json = require('fast-xml-parser').parse;
var jsonObj = xml2json('<gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile">206 555 1212</gd:phoneNumber>', {ignoreNameSpace : true});
console.log(jsonObj.phoneNumber); // "206 555 1212"
//var xml2json=require('fast-xml-parser').parse;
var jsonObj=xml2json('206 555 1212',{ignoreNameSpace:true});
console.log(jsonObj.phoneNumber);/“206 555 1212”
或者,如果您自己制作正则表达式,我建议您使用正则表达式来捕获匹配字符串,正如@DaveWard在其回答中建议的那样,而不是使用
replace

仅基于正则表达式的字符串。您可以将其包含在项目中

//var xml2json = require('fast-xml-parser').parse;
var jsonObj = xml2json('<gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile">206 555 1212</gd:phoneNumber>', {ignoreNameSpace : true});
console.log(jsonObj.phoneNumber); // "206 555 1212"
//var xml2json=require('fast-xml-parser').parse;
var jsonObj=xml2json('206 555 1212',{ignoreNameSpace:true});
console.log(jsonObj.phoneNumber);/“206 555 1212”

或者,如果你自己制作正则表达式,我建议你使用正则表达式来捕获匹配字符串,正如@DaveWard在他的回答中所建议的那样,而不是使用
replace

Jamie Zawinski,1997:“有些人在遇到问题时会想“我知道,我会使用正则表达式。”现在他们有两个问题了。”…:)什么部分在atrib/val的上下文中保持不变?google.com/g/…#mobile呢?该标记中还有其他attr/val吗?这是在xml上下文中吗?是的,当然可以用正则表达式实现。但是,Javascript的正则表达式实现不是很强大;它甚至不能正确处理Unicode。不过,可能会给你一些想法。JamieZawinski,1997:“有些人在遇到问题时会想:“我知道,我会使用正则表达式。”现在他们有两个问题。”…:)在atrib/val的上下文中,哪一部分保持不变?那么google.com/g/…#mobile呢?标签上还有其他属性/价值吗?这是在xml上下文中实现的吗?是的,当然可以用正则表达式实现。然而,Javascript的正则表达式实现不是很强大;它甚至不能正确处理Unicode。尽管如此,我还是可以给你一些想法。这很有帮助,但我就是不能正确理解语法。jquery很有帮助,但我就是不能正确理解语法。jquery是