如果JavaScript中的两个字符之间有空格,我怎么能跳过这个空格?

时间:2017-03-20 16:20:14

标签: javascript

我有文字:

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

任何帮助?

3 个答案:

答案 0 :(得分:1)

我试图为此创建一个正则表达式并且没有成功。如果有人可以做正则表达式,那当然会好得多。但无论如何,如果正则表达式无法帮助我们,我们会回到旧方法。所以这里有一种老式的风格:

[ WITH [ RECURSIVE ] with_query [, ...] ]
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
    * | expression [ [ AS ] output_name ] [, ...]
    [ FROM from_item [, ...] ]
    [ WHERE condition ]
    [ GROUP BY expression [, ...] ]
    [ HAVING condition [, ...] ]
    [ WINDOW window_name AS ( window_definition ) [, ...] ]
    [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
    [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start [ ROW | ROWS ] ]
    [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
    [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]
 where from_item can be one of:

    [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
    ( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
    with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
    function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ]
    function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
    from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ]
 and with_query is:

    with_query_name [ ( column_name [, ...] ) ] AS ( select | values | insert | update | delete )

TABLE [ ONLY ] table_name [ * ]

JSFiddle:https://jsfiddle.net/7jazt7y1/7/

答案 1 :(得分:0)

好的,结果是请求的内容, 请记住它正在搜索与查找弧不同的上下文。另外,这取决于首先修剪弦的条件。详细信息在Snippet中进行了评论。

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);

答案 2 :(得分:0)

我无法摆脱拖尾捕获的群体,但我认为它可以帮助!

这是我的解决方案:

var str = "  first.getage()  person.getinfo( tow.fff(one) , data ) car.getcompany fff  ";

// trim to remove spaces at the begining and at the end
var output = str.trim().split(/[$\s]+(\w+(?:\.\w+)?(?:\((?:[\s\.\w\(\),]*)\))?)\s*/);

// output = ["first.getage()", "person.getinfo( tow.fff(one) , data )", "car.getcompany", "fff", ""]