How can I replace a string that contains line breaks in the document using searchModule.findAll and searchResults.replaceAll?
In my case, when the string in the document contains a line break, the documentEditor.searchModule.findAll(string) method does not return any results. As a result, I'm unable to perform the replacement using documentEditor.searchModule.searchResults.replaceAll(string).
Is there a way to handle and replace text that includes line breaks using the search module in the Syncfusion Document Editor?
Example :-
In my document, a sentence like:
"Unless otherwise stated... delivery of the
Products shall be..."
has a line break between "the" and "Products".
I need to search for "the Products" using documentEditor.searchModule.findAll("the Products"), but it returns no result if a line break is present.
Hi Mathan,
We can achieve your requirement using our DocIo server library Find and Replace option. Then you can load that document using Load() in Import API of Document Editor. Refer to the below code
|
[AcceptVerbs("Post")] [HttpPost] [EnableCors("AllowAllOrigins")] [Route("Import")] public string Import(IFormCollection data) { if (data.Files.Count == 0) return null; Stream stream = new MemoryStream(); IFormFile file = data.Files[0]; int index = file.FileName.LastIndexOf('.'); string type = index > -1 && index < file.FileName.Length - 1 ? file.FileName.Substring(index) : ".docx"; file.CopyTo(stream); stream.Position = 0; Stream stream1 = new MemoryStream();
WordDocument docioDocument = new WordDocument(stream, FormatType.Automatic)) docioDocument.Replace("the\vProducts", "Cycles", true, true); docioDocument.Save(stream1); WordDocument document = WordDocument.Load(stream1, GetFormatType(type.ToLower()));
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document); document.Dispose(); return json; }
|
Document Editor Web services: https://ej2.syncfusion.com/react/documentation/document-editor/web-services-overview
UG: Find and replace in Word document | DocIO | Syncfusion
GitHub samples - DocIO-Examples/Find-and-Replace at main · SyncfusionExamples/DocIO-Examples
Please try it and let us know if you need further assistance.
Regards,
Akshaya
I have a few follow-up questions regarding your proposed approach and the initial problem:
Finding Text with Line Breaks: Your C# example demonstrates how to replace "the\vProducts". Before performing the replacement, how can I first find or verify if this specific string, including the \v manual line break, actually exists within the document using the DocIO library?
Python Code Reference: Our backend is developed in Python. Would it be possible to provide a Python code example that accomplishes the same find-and-replace functionality for text containing line breaks?
Client-Side React Solution: My original goal was to handle this entirely on the frontend. Is there any way to achieve this find-and-replace for strings with line breaks directly within the React Document Editor component, without needing a server-side roundtrip?
I appreciate your guidance on this.
Hi Mathan,
Regarding Finding Text with Line Breaks:
In DocIO, the Replace API is used to replace specific items and executes only
if the particular items are present in the document. If your requirement is to
find text with breaks in the document, kindly refer to the sample below. In
this sample, text with breaks is found using the FindAll API and replaced with
new text.
Regarding Python Code Reference:
DocIO does not support running directly on Python. You can consider the
example below, and instead, run it using a Python wrapper. Kindly refer to the
documentation link below for more information.
How to Create a Word Document using DocIO in ASP.NET Core?
In the CS file below, you can modify the code based on your requirement.
DocIO-Examples/Getting-Started/Python-wrapper/Create-document/Create-document/CreateWordDocument.cs
at main · SyncfusionExamples/DocIO-Examples
Regarding 'client side Find and replace':
Currently we are checking the feasibility of find and replace in client side. So, we will share further details on 24th July 2025.
Regards,
Aravind D
Hi Mathan,
We have anlayzed your requirement ‘Find and replace text with line breaks in Document Editor’ and it will be included in our 2025 Volume 2 SP release which is expected on first week of August
You can track the status of the implementation through the below feedback link:
Find and Replace the text which include line break in JavaScript | Feedback Portal
Regards,
Akshaya
Hi Mathan,
We can achieve your requirement to search the text with line breaks in Document Editor using the findAll() API. Refer to the below code snippet and the sample for the same
|
container.current.documentEditor.search.findAll('the\vProducts');
|
Sample : https://stackblitz.com/edit/react-cva6w8f5?file=index.js
Please try the provided solution and revert with more details if you need further assistance.
Regards,
Akshaya
Thanks for the suggestion.
In my case, I already know the exact text I need to replace — for example "the Products".
The problem is that in the actual document there might be a line break between "the" and "Products", so the plain "the Products" search won’t match.
How would I detect or handle that line break without manually checking the document content?
Ideally, I want to pass "the Products" and still have it match even if it’s stored as:
code for reference:-
const insertClauseToReview = (existingText, newlyAddingText) => {
const container = containerRef.current;
if (!container) return;
const documentEditor = container.documentEditor;
if (!documentEditor || !documentEditor.searchModule) {
return;
}
documentEditor.searchModule.findAll(existingText);
if (documentEditor.searchModule.searchResults.length > 0) {
documentEditor.searchModule.searchResults.replaceAll(newlyAddingText);
}
};
The challenge is:
If there’s a \v or \n between words in the document, the above code doesn’t find it.
I’d like a way to search "the Products" and still match "the\vProducts" or "the\nProducts" automatically.
Is there a way in Syncfusion Document Editor to make findAll() treat spaces as matching line breaks too?
Hi Mathan,
Thank you for your update.
We have created sample to find and replace the text with /without line breaks and possible combinations and share the same in the below link,
Sample : https://stackblitz.com/edit/react-cva6w8f5-tyxizjz1?file=index.js
Steps to run :
1.Click on ‘Find’ to fin the ‘the products’
2. Click on Replace will replace the text.
Please try the provided solution and revert with more details about your use case and the requirement if you still have concerns.
Regards,
Akshaya
Thank you for your response. The provided solution works well for handling line breaks. However, while integrating it, I noticed that in my Word document there are no line breaks but rather a different formatting issue that prevents the text from being identified.
I will share a Loom video link for your reference. Could you kindly review it and advise?
https://www.loom.com/share/5079f64168694bd583693391c07285ce?sid=53224f3c-90e3-4968-8f3e-a326b16ac06d
It appears that there is a visible gap between two lines instead of an actual line break. Could you please suggest an approach to accurately find the text even when such gaps exist?
I will attach the document also for more reference
HI Mathan,
We can reproduce the reported behaviour, we are currently checking on it, we will update you with further details on August 25, 2025.
Regards,
Akshaya
HI Mathan,
We can reproduce the reported behaviour, we are currently checking on it, we will update you with further details on August 25, 2025.
Regards,
Akshaya
HI Mathan,
We are currently checking on the reported issue, we will update you with further details on August 26, 2025. We appreciate your patience until then.
Regards,
Akshaya
Hi Mathan,
We have reviewed the reported issue and identified that the
words ‘the’ and ‘products’ are located in separate paragraphs(see the paragraph
marker in the attached image) within the document.
Due to this structural separation, the findAll() API is unable to
detect the phrase as a continuous match. This behavior is consistent with how
Microsoft Word handles similar cases, where text spanning across paragraph
boundaries is not treated as a single searchable string.
As part of our implementation, we are aligning with this standard behavior to ensure consistency and compatibility with widely accepted document processing practices.
Please let us know if you have any further questions or require additional assistance.
Regards,
Akshaya