我有文字:

first.getage()  person.getinfo( tow.fff(one) , data ) car.getcompany fff


我想得到输出:

first.getage()/person.getinfo( tow.fff(one) , data )/car.getcompany/fff


我的问题,当我按空格分割时,我得到的输出:

first.getage(/)/person.getinfo(/tow.fff(one)/data/,/)/car.getcompany/fff


发生这种情况是因为我在(“()”)之间有空格,所以如果空格出现在圆弧之间,那么如何跳过空格,所以我想要的输出是:

first.getage()/person.getinfo( tow.fff(one) , data )/car.getcompany/fff


有什么帮助吗?

最佳答案

好的,结果就是所要求的,但是请记住,它是在与查找弧不同的上下文中搜索。另外,这取决于首先修剪字符串的条件。在代码段中评论了详细信息。

SNIPPET



// Raw string
var str = ' first.getage()  person.getinfo( tow.fff(one) , data ) car.getcompany ';

// Trim the space off of th start and end of str
var str = str.replace(/(^\s+|\s+$)/g, '');

/* This says:
|| Find any literal fragment that is ".get"
|| Then find everything that's a character before ".get"...
|| until there's a space.
|| Now replace that particular space with:
|| a space, / , and another space
*/
var rgx = /\s\b(?=\w*(?=\.get))/g;

var res = str.replace(rgx, ' \/ ');

console.log(res);

10-07 15:52