text extract

This commit is contained in:
2026-01-17 15:48:43 -06:00
parent 45ee4a337c
commit 97ece8e5cb
3 changed files with 663 additions and 173 deletions

View File

@@ -2858,6 +2858,26 @@ return {
if (result != "he[2][3]o") throw "replace with function failed: " + result
},
test_replace_with_function_limit: function() {
var result = replace("banana", "a", (match, pos) => `[${pos}]`, 2)
if (result != "b[1]n[3]na") throw "replace with function limit failed: " + result
},
test_replace_with_regex: function() {
var result = replace("banana", /a/, "o")
if (result != "bonono") throw "replace with regex failed"
},
test_replace_with_regex_limit: function() {
var result = replace("banana", /a/, "o", 2)
if (result != "bonona") throw "replace with regex limit failed: " + result
},
test_replace_with_regex_function: function() {
var result = replace("hello", /l/, (match, pos) => `[${pos}]`)
if (result != "he[2][3]o") throw "replace with regex function failed: " + result
},
// ============================================================================
// TEXT FUNCTION (Conversion and Slicing)
// ============================================================================
@@ -3520,4 +3540,82 @@ return {
if (result != 42) throw "immediately invoked function failed"
},
test_text_split_text: function() {
var text = "hello world"
var result = array(text, " ")
if (result.length != 2) throw "text split failed"
if (result[0] != "hello") throw "text split failed"
if (result[1] != "world") throw "text split failed"
},
test_text_split_regex: function() {
var text = "hello world"
var result = array(text, /\s+/)
if (result.length != 2) throw "text split failed"
if (result[0] != "hello") throw "text split failed"
if (result[1] != "world") throw "text split failed"
},
test_text_search_text: function() {
var text = "hello world"
var result = search(text, "world")
if (result != 6) throw "text search failed"
},
test_text_search_regex: function() {
var text = "hello world"
var result = search(text, /world/)
if (result != 6) throw "text search failed"
},
test_extract_basic_text: function() {
var text = "hello world"
var result = extract(text, "world")
if (result[0] != "world") throw "extract basic text failed"
},
test_extract_text_not_found: function() {
var text = "hello world"
var result = extract(text, "xyz")
if (result != null) throw "extract not found should return null"
},
test_extract_regex_basic: function() {
var text = "hello world"
var result = extract(text, /world/)
if (result[0] != "world") throw "extract regex basic failed"
},
test_extract_regex_with_capture_group: function() {
var text = "hello world"
var result = extract(text, /(\w+) (\w+)/)
if (result[0] != "hello world") throw "extract regex full match failed"
if (result[1] != "hello") throw "extract regex capture group 1 failed"
if (result[2] != "world") throw "extract regex capture group 2 failed"
},
test_extract_regex_digits: function() {
var text = "abc123def456"
var result = extract(text, /(\d+)/)
if (result[0] != "123") throw "extract regex digits failed"
if (result[1] != "123") throw "extract regex digits capture failed"
},
test_extract_with_from: function() {
var text = "hello hello world"
var result = extract(text, "hello", 1)
if (result[0] != "hello") throw "extract with from failed"
},
test_extract_with_from_to: function() {
var text = "hello world hello"
var result = extract(text, "hello", 0, 10)
if (result[0] != "hello") throw "extract with from to failed"
},
test_extract_regex_case_insensitive: function() {
var text = "Hello World"
var result = extract(text, /hello/i)
if (result[0] != "Hello") throw "extract regex case insensitive failed"
},
}