String and regex replacements #20105
-
Hi folks, I have rewritten all my javascript files from Deno to V, and it's been a breeze! The only thing I am struggling with is a combination of a string + regex replacements for a sanitizing function. function sanitize(query) {
let sanitized = query
.replaceAll('ä', 'ae') // example for string replacement
.replace(/[^\w.]+/g, '-') // example for regex replacement
return sanitized;
} What is the best way to do this in V. In the original script there are a dozen replacements. Thank you very much! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Glad you enjoy it! @penguindark what would be the best way to do this? |
Beta Was this translation helpful? Give feedback.
-
@Brixy I'm supposing that import regex
fn sanitize(query string) string {
mut re_sanitize := regex.regex_opt(r'[^\w.]+') or { panic(0) }
return re_sanitize.replace(query.replace('ä', 'ae'), '-')
}
fn main() {
println(sanitize("bäest everum.bat gones_cedum.jpg poster@conen # pero^märcio"))
}
// Output: baeest-everum-gones_cedum-poster-conen-pero-maercio This code is only an example, it can be optimized better, anyway I'm not sure that regex is the |
Beta Was this translation helpful? Give feedback.
-
I'll answer in two points:
About regex, it is a "small" interpreter that run in your program, it needs to compile your query before execute it. fn my_replace(in_txt string, repl_ch u8) string {
mut buf := []u8{cap:in_txt.len + 1} // buffer for the new string
mut match_seq := 0 // match sequence counter
for ch in in_txt {
// Equivalent of [^\w.]
if (ch >=`a` && ch <=`z`) || (ch >=`A` && ch <=`Z`) || ch == `_` || ch == `.` {
if match_seq > 0 {
// insert a replacement char
buf << repl_ch
match_seq = 0
}
buf << ch
continue
}
match_seq++
}
// If the we are in a match_sequence insert a replacement char
if match_seq > 0 {
buf << repl_ch
}
buf << 0 // C stringterminator 0
return unsafe{cstring_to_vstring(buf.data)}
} This function is very fast then regex and do the same job of your query in an imperative way. |
Beta Was this translation helpful? Give feedback.
@Brixy I'm supposing that
\g
is the global flag form PHP regex, then a possible code can be:This code is only an example, it can be optimized better, anyway I'm not sure that regex is the
right solution for this task.