Find the string using the regex and reuse the specified string with the new surrounding text

I searched for Hi and low, but could not find the exact answer to what I am trying to do ...

I would like to find any text with __ at the beginning and / __ at the end ( i.e. "in the middle of the sentence __this/__ could be underlined, and __this(!) can also/__ be underlined")). Thus, it can be one word or several characters with any characters, including spaces. be different words and combinations - in the same paragraph - starting with __ and ending with / __.

Once discovered, I would like to remove __ and / __ and replace them with HTML - for example, a div tag. So:

__sample string /__ 

it should be:

<div>sample string</div>

I know that I have to use capture groups, but I cannot find a way to do this.

JavaScript: .matchseems to match and put the results in an array - but how do I get back into the string and replace the results found?

JQuery: .replace , , ...

!

+4
2

, String#replace:

s='in the middle of the sentence __this/__ could be underlined, and __this(!) can also/__ be underlined';
var repl = s.replace(/__(.*?)\/__/g, "<div>$1</div>");
//=> in the middle of the sentence <div>this</div> could be underlined, and <div>this(!) can also</div> be underlined
+4

. , . ... . , , , , .

    public static string getBetween(string strSource, string strStart, string strEnd)
    {
        int Start, End;
        if (strSource.Contains(strStart) && strSource.Contains(strEnd))
        {
            Start = strSource.IndexOf(strStart, 0) + strStart.Length;
            End = strSource.IndexOf(strEnd, Start);
            return strSource.Substring(Start, End - Start);
        }
        else
        {
            return "";
        }
    }

    string betweenString = getBetween(sourceString, "__", "/__");

    sourceString = sourceString.Replace("__"+betweenString+"/__", "<div>"+betweenString+"</div>");
0

Source: https://habr.com/ru/post/1534707/


All Articles