2016-06-17 3 views
0

I '('다음 코드정규식 특수 문자를 대체하는

<span id="{{policy.pname}}_query" class="policy-tag">{{policy.policy_groups[0].query}}</span> 

policy.policy_groups[0].query 

의 출력이 tags:(taga || tagb || tagc)

내가 정규식이 첫 번째 문자를 삭제하려면이 , 마지막 ')'을 대체하고 '||' 와 ','

때문에 최종 출력은 tags:taga,tagb,tagc

가 있지만 lucks를 시도합니다. 컨트롤러에서이 작업을 수행 할 수 있지만 html에서는 올바르지 않습니다. 도움을 주시면 대단히 감사하겠습니다.

- 킴벌리

+1

['. "("("," ") .replace (?!.* \)) .replace (/ \ s * \ | \ \ \ s */g, ",")'] (https://jsfiddle.net/Ldvv15ep/) –

+0

또는 더 간단합니다. 다른() 또는 || (/ \ \/\, /, ",")' – mplungjan

+0

@Kim, 모든 것을 제거 하시겠습니까? '('및')'문자열에? –

답변

0

기본적으로이 :

console.log(
 
    'tags:(taga || tagb || tagc)'.replace(/\(|\)/g, "").replace(/\s*\|\|\s*/g, ",") 
 
)

설명 :

.replace(
/<-- open regex 
    \(<-- find literal opening parenthesis 
    | <-- or 
    \) <-- find literal closing parenthesis 
    /g <-- in the whole string 
    , <-- "replace" method delimiter 
    "" <-- replace with empty string 
) 
.replace( 
/<-- open regex again 
    \s* <-- find zero or more spaces 
    \| <-- find a pipe 
    \| <-- find another pipe 
    \s* <-- find zero or more spaces after that 
    /g <-- in the whole string 
    , <-- "replace" method delimiter 
    "," <-- replace with commas 
) 

이 정규식은 경우, 태그에는 괄호가없는 것으로 가정 그렇지 않다면 알려주세요. 이것을 처리하기 위해 날짜를 기입하십시오.

도움이 되었기를 바랍니다.

+0

죄송합니다. {{policy.policy_groups [0 ] .query.replace (/ \ (| \)/g, "") .replace (/ \ s * \ | \ | \ s */g, ",")}} 그러나 작동하지 않았습니다. – Kim

+0

- 매우 자세한 내용은이 정규식에 대한 설명 – Kim

+0

시도 : span id = "{{policy.pname}} _ query"class = "정책 태그"> {{policy.policy_groups [0] .query.replace (/ \ (| \)/g, ",")}} –

0

당신은 또한 정규 표현식없이이 작업을 수행 할 수있는 대체 필터 (https://www.npmjs.com/package/angularjs-filters)

<span id="{{policy.pname}}_query" class="policy-tag">{{policy.policy_groups[0].query | string.replace:"/([\(\)]| *\|{2} *)/": "," }}</span>

+0

{{policy.policy_groups [0] .query.replace (/ \ (| \)/g, "" , ")}}하지만 작동하지 않았습니다. – Kim

0

을 사용할 수 있습니다 가정. 좀 더 자세한 정보를 제공하지만 읽기 및 유지 관리가 쉽습니다.

var string = policy.policy_groups[0].query; 
var first = string.indexOf('('); 
var last = string.indexOf(')'); 
string = string.substring(0, first) + string.substring(first + 1, last) + string.substring(last + 1); 
string.split(' || ').join(','); 
관련 문제