[英]Regex that remove tags and specific string
I have a Caml query that i must sanitize.我有一个必须清理的 Caml 查询。 I collect datas from a form using javascript.
我使用 javascript 从表单中收集数据。 For this I map the object with the field names:
为此我 map object 与字段名称:
...
query += Object
.entries(fields)
.map(([k, v]) => (('beginning' + k) in data) && (('ending' + k) in data)
? `<Geq><FieldRef Name='DATE_FACTURE'/><Value IncludeTimeValue='FALSE' Type='DateTime'> '${data['beginning' + k]}' <Leq><FieldRef Name='DATE_FACTURE'/><Value IncludeTimeValue='FALSE' Type='DateTime'> '${data['ending' + k]}'`
: `<Contains><FieldRef Name="${v}" /><Value Type='Text' >'${data[k]}'</Value></Contains>`
)
.join(' </And> ');
query += "</Where></Query></View>";
This function allow me to generate the query but i want to remove the empty values.这个 function 允许我生成查询,但我想删除空值。 For example, if i send the form with only one field setted I obtain a string:
例如,如果我发送的表单只设置了一个字段,我会得到一个字符串:
<View>
<Query>
<Where>
<Contains>
<FieldRef Name="date" /><Value Type='Text' >'undefined'</Value>
</Contains>
</And>
<Contains>
<FieldRef Name="FileLeafRef" />
<Value Type='Text' >'undefined'</Value>
</Contains>
</And>
<Contains>
<FieldRef Name="CODE1" /><Value Type='Text' >'undefined'</Value>
</Contains>
</And>
<Contains>
<FieldRef Name="CODE2" />
<Value Type='Text' >'undefined'</Value>
</Contains>
</And>
<Contains>
<FieldRef Name="CODE3" />
<Value Type='Text' >'Hello World'</Value>
</Contains>
</Where>
</Query>
</View>
My objective is to remove:我的目标是删除:
<Contains>
<FieldRef Name="date" /><Value Type='Text' >'undefined'</Value>
</Contains>
</And>
<Contains>
<FieldRef Name="FileLeafRef" />
<Value Type='Text' >'undefined'</Value>
</Contains>
</And>
<Contains>
<FieldRef Name="CODE1" /><Value Type='Text' >'undefined'</Value>
...
I should not have the "contains" with undefined values.我不应该有未定义值的“包含”。 I found out how to remove the string undefined ( (\w undefined\w \b) ).
我发现了如何删除未定义的字符串( (\w undefined\w \b) )。 But i'm stuck at this stage.
但我被困在这个阶段。
This is not something that regex is meant to do.这不是正则表达式要做的事情。
You can use jQuery to filter it, something like this:您可以使用 jQuery 对其进行过滤,如下所示:
$(query).find('Contains Value')
.filter($singleContains => $(singleContains).text() != "'undefined'")
Or with plain javascript:或者使用普通的 javascript:
query.querySelectorAll('Contains Value')
.filter(singleContains => singleContains.innerText != "'undefined'")
I simply checked in the map the values:我只是检查了 map 的值:
Object
.entries(fields)
.map(([k, v]) => {
if (data[k] == null)
query += ``;
else
query += `<Contains><FieldRef Name="${v}" /><Value Type='Text' >'${data[k]}'</Value></Contains>`;
})
.join(' </And> ');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.